Save and close incrementing open figures
9 views (last 30 days)
Show older comments
Life is Wonderful
on 19 Jul 2023
Commented: Steven Lord
on 20 Jul 2023
Hello there,
I have a for loop for evaluating the imagedatastore photos, a for loop for examining all date store figures, and the figures open with an incremental numeric value while being examined. Each loop iteration opens approximately 22 figures, and I have approximately 30 photos in the data store to analyse. So, if I don't close the figure, I'll run out of graphics memory owing to the 30 photos x 20 figures analysis, which means I'll have 600 figures open at once.
So I'd expect a time 24 figure to open and increment the figure number, and once a for loop iteration is complete, I should save and then close the figures to avoid running into problems.
I am using below code to save and close the figures . Can I get the help here ?
Thank you
NumFigs = 30;
for i = 1:length(NumFigs)
figure;
FolderName = outputFolder; % using my directory to store open images
FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
figHandles = get(groot, 'Children');
for iFig = 1:length(FigList)
FigHandle = FigList(iFig);
FigName = num2str(get(FigHandle, 'Number'));
set(0, 'CurrentFigure', FigHandle);
saveas(FigHandle, fullfile(FolderName,strcat(FigName, '.png')));
end
% close the open figure
if FigHandle.Number < 2 % this corresponds to GUI, so avoid closing first two figures
fprintf('cant close figs');
else
for j=1:length(figHandles)
close(figHandles(j));
end
end
end
0 Comments
Accepted Answer
Steven Lord
on 19 Jul 2023
Don't use findobj. When you create a figure, store its handle. When you want to close a figure, use the stored handle.
listOfFigures = gobjects(5, 5);
for k = 1:25
% Store the handle
listOfFigures(k) = figure;
end
% Close just figure 13
close(listOfFigures(13));
2 Comments
Steven Lord
on 20 Jul 2023
listOfFigures = gobjects(1, 10);
for whichone = 1:10
listOfFigures(whichone) = figure('Name', "Figure # " + whichone, ...
'UserData', whichone.^2);
end
Now let's find the 7th figure, the one with UserData of 49.
allUserdata = [listOfFigures.UserData]
seventh = find(allUserdata == 49)
Check that we have in fact gotten the 7th figure.
listOfFigures(seventh).Name % Should be "Figure # 7"
I'm guessing you're trying to use the Number property (or the numeric value of the handle) in your filename. Don't. You have (at best) limited control over that property or the handle's numeric value. Let's say we deleted one of those handles, let's say figure 3, and create a new figure as element 11 of the list.
delete(listOfFigures(3));
listOfFigures(11) = figure('UserData', 121);
listOfFigures(11).Number
double(listOfFigures(11))
The number 3 was recycled for the new figure. And no, you cannot set the Number property of the figure. If you know there's no figure with a certain number you could call figure with that number as input and MATLAB will create it, but if it does exist now operations that write to the current figure will write to that existing figure.
f = figure(42);
f.Number
f = figure(8);
f.Name = "overwritten";
listOfFigures(8).Name % No longer "Figure # 8"
listOfFigures(12) = figure('Number', 12);
More Answers (0)
See Also
Categories
Find more on Graphics Object Programming 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!