Save and close incrementing open figures
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
Steven Lord
2023년 7월 19일
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
Steven Lord
2023년 7월 20일
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);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!