How can i save instantaneously all plots of a script?

10 次查看(过去 30 天)
Hello,
I have a script in which several graphs are created. I would like these to be automatically saved in predefined folders that I decide. For now I'm writing below each figure:
fig = gcf;
ax = fig.CurrentAxes;
ax.FontSize = 17;
fig.PaperUnits = 'centimeter';
fig.PaperPosition = [0 0 29.7 21];
saveas (fig, [pwd, '/ MATLAB / Immagini_tesi_Matlab / OBIDOS / RELIABILITY / NA_SCENARIO / Relmodel.png']);
I wondered if there is a way to extend this automatic procedure to all the figures with a single command.
Thanks in advance

回答(1 个)

Jan
Jan 2018-3-7
There is no single command, but you can write a function to do this:
function SaveAllFigures
Folder = fullfile(pwd, '/MATLAB/Immagini_tesi_Matlab/OBIDOS/RELIABILITY/NA_SCENARIO/');
AllFigH = allchild(groot);
for iFig = 1:numel(AllFigH)
fig = AllFigH(iFig);
ax = fig.CurrentAxes;
ax.FontSize = 17;
fig.PaperUnits = 'centimeter';
fig.PaperPosition = [0 0 29.7 21];
FileName = [fig.Title, '.png'];
saveas(fig, fullfile(Folder, FileName));
end
end
Maybe the file name should be numbered instead:
FileName = sprintf('Image%03d.png', iFig);
  • fullfile is safer than the concatenation of the char vectors.
  • I have removed the spaces around the file separators in the folder name.
  • Relying on the current folder by pwd is dangerous: it can be changed by callbacks of GUIs or timers unexpectedly. Better provide the base folder as input argument to your function.
  5 个评论
Jan
Jan 2018-3-8
编辑:Jan 2018-3-8
@Gianluca Borgna: Again, please do not post comments in the section for answers, because this will look confusing if more answers are given and voting reorders them.
Maybe you mean:
if choice == 1
FolderName = 'NA_SCENARIO';
elseif choice == 2
FolderName = 'PREV_SCENARIO'
elseif ...
end
SaveAllFigures(FolderName);
And a slightly modified function:
function SaveAllFigures(FolderName)
Folder = fullfile(pwd, '/MATLAB/Immagini_tesi_Matlab/OBIDOS/RELIABILITY/', FolderName);
...

请先登录,再进行评论。

类别

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