saving multiple figures to a named path

3 Ansichten (letzte 30 Tage)
Stelina
Stelina am 26 Apr. 2014
Bearbeitet: dpb am 29 Aug. 2021
Dear community,
I have a script that produces multiple figures and I would like to save them automatically to a named folder. I have looked to similar examples at this forum but I still cannot figure out how to create multiple handles. The result is that, although the figures are plotted and seem to be saved in the format I have specified (.bmp), only the lastly created figure is saved in the specified folder.
######################################################################
The code I am using is the following: (this part of the code rests inside a for loop, with n being the counter)
figure(n)
set(gcf)
set(gca)
plot((5:5:300),mean(A,1)); % we take an average plot of
%the 300ms after all the 40 stimulations of the stimulated
%electrode.
title(['File: ' filename ' Stim electr: ' num2str(StimElectr(n))]);
xlabel('Latency [ms]'); ylabel('Count'); grid on
fpath='G:\test'
saveas(figure(n),fullfile(fpath,'PSTHPlots'),'bmp')
Any help appreciated! Thank you, Stella
  2 Kommentare
Marcus Tan
Marcus Tan am 29 Aug. 2021
Hi @Stelina I am working on a project that requires me to save multipl figure into bmp format to a named path. Really appreciate your advise if you could share with me how you do it. I tried following what u did by doing figure(n) but it keep giving me error.
dpb
dpb am 29 Aug. 2021
Bearbeitet: dpb am 29 Aug. 2021
The <Answer> I gave below does work for the specific case; it's not possible to know where you went wrong without your code and error.
That probably would be more suitable as a new Question rather than follow-up here; one could hazard a guess you've used some other variable than n in a loop or the idea outlined needs to be tailored to your specific code rather than copied verbatim.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 26 Apr. 2014
...
saveas(figure(n),fullfile(fpath,'PSTHPlots'),'bmp')
They're all being saved in the location requested but you're not updating the file name so the end result is you're overwriting N-1 previous with the Nth.
You need to update the filename to a unique name for each; perhaps using some variant of the name you've created for the title above.
  1 Kommentar
Stelina
Stelina am 28 Apr. 2014
Yeap, I created a name that gets updated for every iteration by horzcat the num2str(filename) and num2str(StimElectr(n)) and it works, thank you!
Stella

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Printing and Saving finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by