Error in savefig command

2 visualizaciones (últimos 30 días)
Konstantinos
Konstantinos el 19 de Mzo. de 2015
Editada: Anudeep Kumar el 4 de Jun. de 2025
I used the following code in matlab in odrer to save my figures:
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.
but I get this: "Undefined function 'savefig' for input arguments of type 'double'." My version of Matlab does not have the command "savefig". Can anyone help me by giving me the code of this command, or propose to me an alternative way of saving my plots automatically in a folder of my choice ?

Respuestas (1)

Anudeep Kumar
Anudeep Kumar el 4 de Jun. de 2025
Editada: Anudeep Kumar el 4 de Jun. de 2025
Hey Konstantinos,
I believe the error being thrown could be due to 'savefig' being called with an incorrect input type.
As per the documentation, 'savefig' expects a figure handle, but it could be the case that 'FigHandle' is not a valid figure handle.
To resolve this error, make sure 'FigHandle' is a valid figure handle and not a double or invalid reference.
In case you are using MATLAB version prior to R2013b, 'savefig' will not be available. You can use 'saveas' instead.
Here is an alternative code using 'saveas' function
FolderName = tempdir; % Destination folder
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
for iFig = 1:length(FigList)
FigHandle = FigList(iFig);
FigName = get(FigHandle, 'Name');
% Save the figure as .fig using saveas
saveas(FigHandle, fullfile(FolderName, [FigName '.fig']));
end
I have attached the documentation for 'savefig' :
and 'saveas' for your reference
Hope it helps!

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by