questions about data save

Dear all, We do some psychophysics experiments now. When work on the programs, meet a question.
We save our trial data use struct. In every session we want to save the data to disk trial by trial.

But we can¡¯t add new arrays to an exist mat file. Could we append every trial data to an exist file.

For example:

data.starttime=0; data.endtime=100

save data.mat data;

clear all



data.starttime=10; data.endtime=200



now, how can we append data to data.mat let us can get file like this

data(1).starttime=0; data(1),endtime=100

data(2).starttime=10;data(2).endtime=200
Thank you, I think I have got the point O(¡É_¡É)O

--- In psychtoolbox@yahoogroups.com, "todd_horowitz" <toddh@...> wrote:
>
> What I do is to use a struct array and just save it every trial. There's no need to append, because there's no significant cost to saving a 1x100 struct vs a 1x1 struct. You just save the whole thing every time. You record the data by addressing data(trial).starttime, for example.
>
> Todd
>
>
>
> --- In psychtoolbox@yahoogroups.com, "Diederick C. Niehorster" <dcnieho@> wrote:
> >
> > The better way would be to rework the structure of your program so
> > that the data struct is not cleared every trial and then save the data
> > only at the end of your run, not each trial. That would require
> > pregenerating the array of structs and accessing
> > data(trialnum).starttime instead of data.starttime in your experiment
> > code.
> >
> > If you really want to save every trial appending, then you'll meet a few issues.
> > When you load your .mat again, the variable 'data' in there will
> > overwrite the data variable of current scope--the one you want to
> > save. You would thus have to do something like this:
> >
> > datanew = data; % keep a copy that does not get overwritten
> > load data.mat % load from file--'data' now contains data from previous trials
> > data = [data datanew]; % append data from current trial to previous data
> > save data.mat data % save it again.
> >
> > I'd go for the first solution, this makes your code unnecessarily complex.
> >
> > -Diederick
Depending on the total number of trials you have, another strategy is instead of saving your full struct after each trial, save it after a given number of trials. For example, we're doing classification image experiments where there is a rest period (e.g. blank screen with `Take a break and press a key to continue') implemented every 200 trials. During the rest period the trial information struct is saved, hence if the computer crashes after 2199 trials, you lose 199 trials instead of 2199 trials, yet the overhead of partial backups is minimal. Also at the end of all trials the struct is saved. Hence I get a series of files such as:

BD-4-05.07.09-19.36.15-tmp-200.mat
BD-4-05.07.09-19.38.49-tmp-400.mat
BD-4-05.07.09-19.41.31-tmp-600.mat
BD-4-05.07.09-19.44.59-tmp-800.mat
BD-4-05.07.09-19.48.02-tmp-1000.mat
BD-4-05.07.09-19.50.54-tmp-1200.mat
BD-4-05.07.09-19.54.20-tmp-1400.mat
BD-4-05.07.09-19.57.35-tmp-1600.mat
BD-4-05.07.09-19.57.37-1600.mat

Which includes a trial condition string (`BD-4`), a timestamp, and the number of trials included in the matrix. This is done in the code by:

%nn is the trial number, data is the struct containing the trial information

if(mod(nn,200) == 0)

%Also save current data in a temporary file
filename = [data.title '-' datestr(now,'dd.mm.yy-HH.MM.SS') '-tmp-' int2str(nn) '.mat'];
save(filename,'data');
end

data is not cleared after 200 trials. However I do have a function to stitch together two structs (say, from two different sessions), which is:

function [data] = mergeDataSets(names)
load(names{1});
tmp = data;
for ii = 2:length(names)
load(names{ii});
fn = fieldnames(data);
for jj = 1:length(fn)
val = data.(fn{jj});
if isnumeric(val) && length(val) > 1
tmp.(fn{jj}) = cat(1,tmp.(fn{jj}),data.(fn{jj}));
end
end
end
data = tmp;
end

This runs through all the keys of the structs and merges the vectors/matrices making them up along the first dimension (e.g it works for column vectors but not row vectors) assuming they contain numeric data.

Patrick Mineault

On Mon, Jul 6, 2009 at 5:38 AM, e_flister <e_flister@...> wrote:
> > What I do is to use a struct array and just save it every trial. There's no need to append, because there's no significant cost to saving a 1x100 struct vs a 1x1 struct. You just save the whole thing every time.

just a word of caution - using this strategy makes save time linear in the number of trials and can become quite noticeable (and contributes to incomparability of trials early vs. late in a session). we have an automated system for training rats in their home cages, and they self-pace their trials, peaking around 1 trial/sec. our trial records are large and our sessions are typically many hundreds of trials long. by the end of sessions, save times were dominating trials, until we switched to an -append strategy. this meant generating a unique variable name for each trial -- lame!

even more lame is that loading struct arrays (or other dynamic structures like cells) from files is extremely slow -- apparently .mat's don't cache the memory layout of variables, so matlab sequentially allocates each member of a struct array as it is read. in our case, analyzing several months of daily records stored as struct arrays is intractable, so we have to maintain an intermediate file compiled into simple vectors. ugh!

-e



------------------------------------

Post your message to: psychtoolbox@yahoogroups.com
Please indicate OS9, OSX, or WIN version, and include your full name.
Denis Pelli, David Brainard, and Allen Ingling.
http://psychtoolbox.org
Yahoo! Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/psychtoolbox/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/psychtoolbox/join
(Yahoo! ID required)

<*> To change settings via email:
mailto:psychtoolbox-digest@yahoogroups.com
mailto:psychtoolbox-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
psychtoolbox-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/