Saving a preallocated matrix within a nested for loop
1 view (last 30 days)
Show older comments
Code is below. I am trying to save the SpectroBey matrix before the nested loop to correspond with the files that are being pulled in within the first loop. So in the end, I would like to have saved SpectroBey1, SpectroBey2, Spectrobey3....and so on.
for j=1:6
SpectroBey=[];
for jj=1:length(tslide)
g=exp(-width*(t-tslide(jj)).^2);
Pg=g.*P; Pgf=fft(Pg);
SpectroBey=[SpectroBey;abs(fftshift(Pgf))];
end
[m,n]=size(SpectroBey);
SpectroBey=reshape(SpectroBey,m*n,1);
save(sprintf('SpectroBey%d', j), 'SpectroBey')
end
0 Comments
Answers (1)
Jan
on 6 Mar 2015
Edited: Jan
on 6 Mar 2015
Your code does explicitly not use a pre-allocation. Therefore the tag and the title is misleading. The code contains a lot of details, which do not concern the question. It is more convenient for the readers if you concentrate on the problem.
As far as I understand your message (what exactly is your question?). all you need is this line:
save(sprintf('SpectroBey%d', j), 'SpectroBey')
You find further details if you read the FAQ section: How to process files in a loop. Ask your favorite search engine for "Matlab FAQ" or search in this forum.
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!