Each time I run my code it produces 100 figures. So, I have to waste my time and save each one of them. Is there a command that can do that work for me by saving all the figures at once?

1 Kommentar

Hira
Hira am 27 Sep. 2022
m=1;
title("S31 Plot Measurement Number ("+m+")")
xlabel('Delay')
ylabel('Mag(S31)')
saveas(gcf,"S31_plot_"+name+"_Measurement_No_"+m+".fig")
close(gcf)
m=m+1;

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 11 Mär. 2015

15 Stimmen

No, there is no such command. But it is easy to write one:
FolderName = tempdir; % Your destination folder
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
for iFig = 1:length(FigList)
FigHandle = FigList(iFig);
FigName = get(FigHandle, 'Name');
savefig(FigHandle, fullfile(FolderName, FigName, '.fig'));
end
Adjust the FigName to your needs.

5 Kommentare

Konstantinos
Konstantinos am 19 Mär. 2015
I used this code and I get this: "Undefined function 'savefig' for input arguments of type 'double'." I have matlab 2013b edition, if it matters. can you help me ?
John Sunwoo
John Sunwoo am 8 Dez. 2018
Bearbeitet: John Sunwoo am 8 Dez. 2018
I think the code needs 'brackets'.. for fullfile(). See below
FolderName = tempdir; % Your destination folder
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
for iFig = 1:length(FigList)
FigHandle = FigList(iFig);
FigName = get(FigHandle, 'Name');
savefig(FigHandle, fullfile(FolderName, [FigName, '.fig'])); %<---- 'Brackets'
end
Mango Wang
Mango Wang am 1 Feb. 2019
Just to complement, there is no need to use for loop to save them one by one.
savefig(FigList,filename) could save all the handles together in one file.
Lars Abrahamsson
Lars Abrahamsson am 18 Mai 2020
I noticed one "problem" when saving all figures into one file.
When loading them back with "openfig" the numbers/order of the figures becomes revered.
Why is that? Can anything be done to counteract that?
Brandon Laflen
Brandon Laflen am 19 Mai 2020
If they load backwards, I'm guessing findobj is LIFO. Maybe try
savefig(FigList(end:-1:1),filename)
instead?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Luke Shaw
Luke Shaw am 30 Nov. 2018
Bearbeitet: Luke Shaw am 30 Nov. 2018

12 Stimmen

Missed a make current step: set(0, 'CurrentFigure', figureHandle)
FolderName = tempdir; % Your destination folder
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
for iFig = 1:length(FigList)
FigHandle = FigList(iFig);
FigName = num2str(get(FigHandle, 'Number'));
set(0, 'CurrentFigure', FigHandle);
savefig(fullfile(FolderName, [FigName '.fig']));
end

5 Kommentare

Tanziha Mahjabin
Tanziha Mahjabin am 17 Feb. 2020
Hi,
i have already saved figures in a folder. how can i make a gif in matlab combining all of them?
Ali Awada
Ali Awada am 16 Okt. 2021
Bearbeitet: Ali Awada am 16 Okt. 2021
Hi Luke,
Thanks for the code works perfectly.
I have a question:
how can i save the figure by the title i have given?
Your code saves them as "1.fig" where 1 corresponds to figure(1) in the code.
manvir kaur
manvir kaur am 6 Jun. 2022
i have same issue, this code works perfectly but i want to save figures in png format. So how to do that. Thanks
Nabil Mederbel
Nabil Mederbel am 11 Jun. 2022
Hi guys,
I tried to save figures with '.eps' format ...didnt work.
any idea ? thx
This worked for '.png' format, it should work for whatever format you want.
FolderName = tempdir; % Your destination folder
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
for iFig = 1:length(FigList)
FigHandle = FigList(iFig);
FigName = num2str(get(FigHandle, 'Number'));
set(0, 'CurrentFigure', FigHandle);
saveas(FigHandle,fullfile(FolderName, [FigName '.png'])); %Specify format for the figure
end

Melden Sie sich an, um zu kommentieren.

Tanveer
Tanveer am 18 Sep. 2022

4 Stimmen

FolderName = 'xx'; % Your destination folder
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
for iFig = 1:length(FigList)
FigHandle = FigList(iFig);
FigName = ['Fig' num2str(iFig)];
savefig(FigHandle, fullfile(FolderName, [FigName '.fig']));
saveas(FigHandle, fullfile(FolderName, [FigName '.png']));
% saveas(FigHandle,filename,formattype)
end
Mehri Mehrnia
Mehri Mehrnia am 3 Aug. 2022

0 Stimmen

Based on the answers, it means there is no 1 line of code which can save all open plots?

1 Kommentar

Hira
Hira am 27 Sep. 2022
m=1;
title("S31 Plot Measurement Number ("+m+")")
xlabel('Delay')
ylabel('Mag(S31)')
saveas(gcf,"S31_plot_"+name+"_Measurement_No_"+m+".fig")
close(gcf)
m=m+1;

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Printing and Saving finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by