Save Figure Using First Subplot Name

1 view (last 30 days)
Brandon Allred
Brandon Allred on 11 Jan 2022
Answered: Walter Roberson on 11 Jan 2022
I currently have a code that outputs a number of figures. I am trying to find a way for me to save the files automatically using the title of the figure as the file name. The following code works for a figure with a single plot, but it returns an error if there are multiple subplots involved.
FolderName = 'C:\\User\Documents'; % Destination folder
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
for i = 1:length(FigList)
FigHandle = FigList(i);
ax = findobj(FigHandle, 'type', 'axes');
FigName = ax.Title.String;
savefig(FigHandle, fullfile(FolderName, [FigName '.fig']));
end

Answers (1)

Walter Roberson
Walter Roberson on 11 Jan 2022
FigName = ax(end).Title.String;
The first axes created in the figure will be the last axes object in Children order (unless you modify the order somehow.)

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!