MATLAB: Saving multiple figures to a named path

saveas multiple figures

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

Best Answer

...
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.