We want to save data as it is generated, and to save it as a .mat file. Since every trial can be of a different length, a simple matrix will not suffice.
We have the following two options:
cell_data.x{1} = randn(1e4,1);
cell_data.y{1} = randn(1e4,1);
% save between trials
cell_data.x{2} = randn(3e4,1);
cell_data.y{2} = randn(3e4,1);
and so on.
The other option is:
struct_data(1).x = randn(1e4,1);
struct_data(1).y = randn(1e4,1);
% save between trials
struct_data(2).x = randn(3e4,1);
struct_data(2).y = randn(3e4,1);
After some simple testing, the second format is slightly faster. Since it's also simpler, this is the format we should use.
% test of different data formats
ntrials = 20;
trial_length = floor(1e3 + 1e5*rand(ntrials,1));
% cell_data
disp('Testing cell data')
clear cell_data
cell_data.x{1} = [];
save('cell_data.mat','cell_data','-v7.3')
tic
for i = 1:ntrials
cell_data.x{i} = randn(trial_length(i),1);
cell_data.y{i} = randn(trial_length(i),1);
cell_data.z{i} = randn(trial_length(i),1);
% save
save('cell_data.mat','cell_data','-append')
end
toc
% structure array
disp('Testing structure data')
clear sdata
sdata(1).x = [];
save('sdata.mat','sdata','-v7.3')
tic
for i = 1:ntrials
sdata(i).x = randn(trial_length(i),1);
sdata(i).y = randn(trial_length(i),1);
sdata(i).z = randn(trial_length(i),1);
% save
save('sdata.mat','sdata','-append')
end
toc
delete('sdata.mat')
delete('cell_data.mat')