Error in savefig command

6 次查看(过去 30 天)
Konstantinos
Konstantinos 2015-3-19
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 ?

回答(1 个)

Anudeep Kumar
Anudeep Kumar 2025-6-4
编辑:Anudeep Kumar 2025-6-4
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!

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by