Clear Filters
Clear Filters

Save and close incrementing open figures

11 views (last 30 days)
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)
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')));
% close the open figure
if FigHandle.Number < 2 % this corresponds to GUI, so avoid closing first two figures
fprintf('cant close figs');
for j=1:length(figHandles)

Accepted Answer

Steven Lord
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;
% Close just figure 13
Life is Wonderful
Life is Wonderful on 20 Jul 2023
This does not appear to be what I am looking for.
I have a datastore that contains 30 image files.
I analyse each file and graph the results. I plot 20 separate figures from each data store image, yielding 30 x 20 = 600 open figures.
If I close the first 25 figures, then the next image data store picture restarts from figure 1 and overwrites previously saved figures, according to your proposal.
So, could you perhaps assist me with reconstructing the logic?
Thank you !!
Steven Lord
Steven Lord on 20 Jul 2023
listOfFigures = gobjects(1, 10);
for whichone = 1:10
listOfFigures(whichone) = figure('Name', "Figure # " + whichone, ...
'UserData', whichone.^2);
Now let's find the 7th figure, the one with UserData of 49.
allUserdata = [listOfFigures.UserData]
allUserdata = 1×10
1 4 9 16 25 36 49 64 81 100
seventh = find(allUserdata == 49)
seventh = 7
Check that we have in fact gotten the 7th figure.
listOfFigures(seventh).Name % Should be "Figure # 7"
ans = '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.
listOfFigures(11) = figure('UserData', 121);
ans = 3
ans = 3
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);
ans = 42
f = figure(8);
f.Name = "overwritten";
listOfFigures(8).Name % No longer "Figure # 8"
ans = 'overwritten'
listOfFigures(12) = figure('Number', 12);
Error using figure
Unable to set the 'Number' property of class ''Figure'' because it is read-only.

Sign in to comment.

More Answers (0)


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!