How can i save instantaneously all plots of a script?
Mostra commenti meno recenti
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 Commento
Jan
il 7 Mar 2018
I have edited your message to make the code readable. Please read http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup.
Risposte (1)
Jan
il 7 Mar 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 Commenti
Jan
il 7 Mar 2018
[MOVED from section for answers]
Gianluca Borgna wrote:
Amazing!
But the problem is also that i have different folders, not just NA_Scenario/.. How can insert in the code these different folders?
Where do i call the function? At the end of the script?
[Please post comments in the section for comments.]
Jan
il 7 Mar 2018
You do not have to insert the code in the folders. What do you want to do exactly? Currently you asked for saving all open figures to one folder. What do you want to do instead? Does each image file needs a different destination folder? If so, how is this folder determined?
Stephen23
il 8 Mar 2018
at the beginning in my script you are asked to insert as input a number in the command window to determine a scenario with respect to another (if choice == 1; .....; if choice == 2; ...).
I would like that depending on the scenario the figures were saved in different folders:
NA_SCENARIO; PREV_SCENARIO; CORR_SCENARIO;
@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
il 8 Mar 2018
Categorie
Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!