MATLAB: How to save a matrix output from a single iteration of a loop

for loopmatrix arrayvariables

I have a code that is looping through a set of folder and performing operations on the files in that folder. The loop structure is shown below:
peakposarray = zeros(N,50); % 50 is for the number of currents
numpeaksarray = zeros(N,50);
vecarray = zeros(6,2);
veclength = 0;
for k = 1:NSpec % loop through each folder in directory
FolderName = SpectrumFileInfo(k).name;
Direc = dir([FolderName '\I*.csv']);
[~,idx] = sort([Direc.datenum]);
Direc = Direc(idx);
DataPointsSpec = length(Direc)-2;
for kk = DataPointsSpec+1:-1:2 % loop through each file in each folder - reverse order so high to low current
SpectrumData = PeakFit([FolderName '\' Direc(kk).name]); % atm this just does csvread
Wavelength=SpectrumData(2:end-1,1); %ignore first and last point
Intensity=SpectrumData(2:end-1,2);
% peak fitting function - fits up to 6 peaks
[pks locs w]=findpeaks(Intensity,'NPeaks',6,'MinPeakProminence',1*10^-3,'MinPeakDistance',4); % seems about the right parameters to only get the modes and no other noise peaks
numpeaks = length(pks); % number of peaks
numpeaksarray(k,kk) = numpeaksarray(k,kk)+numpeaks;
lambda = Wavelength(locs); % wavelength of each peak
vec = [lambda,pks];
veclength(kk) = length(vec);
veclengthordered = fliplr(veclength);
if veclengthordered(kk)<veclengthordered(kk-1)
disp('do not match')
else
disp('do match')
end
end
end
The variable 'vec' inside the second for loop is a n*2 matrix that stores the position and height of each peak found in the data. This varies between files. The if loop compares the size of 'vec' in the current loop to the size of 'vec' in the previous loop. However, I need to be able to compare the actual data in 'vec'. I get a problem when trying to store this data because I have a matrix output for 'vec' rather than a single number, so I can't store something like vec(kk). How can I store the output matrix of 'vec' from the previous loop so that I can compare it to the data in the current loop?

Best Answer

x={[1:5]; %an example
[linspace(0,1,5)]};
y=cell(1,size(x,1)); %pre-allocation
for i = 1:size(x,1)
y{i}=[x{i}].^2; % just use cells to store which is much better
end
celldisp(y)
or
[y{:}] % -> double array