saving multiple figures to a named path

1 次查看(过去 30 天)
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 个评论
Marcus Tan
Marcus Tan 2021-8-29
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 2021-8-29
编辑:dpb 2021-8-29
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.

请先登录,再进行评论。

采纳的回答

dpb
dpb 2014-4-26
...
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 个评论
Stelina
Stelina 2014-4-28
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by