Save and close incrementing open figures
Show older comments
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
Accepted Answer
More Answers (0)
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!