Error in savefig command

2 views (last 30 days)
Konstantinos
Konstantinos on 19 Mar 2015
Edited: Anudeep Kumar on 4 Jun 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 ?

Answers (1)

Anudeep Kumar
Anudeep Kumar on 4 Jun 2025
Edited: Anudeep Kumar on 4 Jun 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!

Categories

Find more on Printing and Saving in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!