How can i save instantaneously all plots of a script?

9 visualizaciones (últimos 30 días)
Gianluca Borgna
Gianluca Borgna el 7 de Mzo. de 2018
Comentada: Gianluca Borgna el 8 de Mzo. de 2018
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

Respuestas (1)

Jan
Jan el 7 de Mzo. de 2018
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 comentarios
Jan
Jan el 8 de Mzo. de 2018
Editada: Jan el 8 de Mzo. de 2018
@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);
...
Gianluca Borgna
Gianluca Borgna el 8 de Mzo. de 2018
Thanks, it works perfectly.

Iniciar sesión para comentar.

Categorías

Más información sobre Printing and Saving en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by