Saving a preallocated matrix within a nested for loop

1 Ansicht (letzte 30 Tage)
jpers
jpers am 6 Mär. 2015
Bearbeitet: jpers am 6 Mär. 2015
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

Antworten (1)

Jan
Jan am 6 Mär. 2015
Bearbeitet: Jan am 6 Mär. 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 Kommentar
jpers
jpers am 6 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by