Saving a preallocated matrix within a nested for loop

1 view (last 30 days)
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

Answers (1)

Jan
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.
  1 Comment
jpers
jpers on 6 Mar 2015
Thank you for your response. I apologize I thought the line before the nested loop, SpectroBey=[], was pre-allocating. So specifically, I would also like for the SpectroBey matrix to be named for each iteration of the first loop.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!