Saveas function changes behaviour of Subplot in subsequent iterations of a loop
1 回表示 (過去 30 日間)
古いコメントを表示
Ruairi Gallagher
2020 年 5 月 27 日
コメント済み: Subhadeep Koley
2020 年 5 月 28 日
Hi,
I have a loop that that runs an analysis and plots data on a 1 by 3 subplot figure.
At the end of each loop, a dialog box offers the option to either "Continue", or "Continue & Save". In either case, the next iteration begins using new data, and a new subplot is generated. If only "Continue" is selected, the code works perfectly everytime in each subsequent iteration.
However, if "Save & Continue" is selected, the figure is saved as expected, but every subsequent iteration fails to generate the correct subplot (only the first of the three subplots are generated, and the remaining space is blank).
if strcmpi(response, 'Continue & Save')
FG=figure(1);
title=sprintf("../day_acoustic/Cluster_Graphs/EchoCluster_%i.png",HAC);
saveas(FG,title);
%export_fig FG title; I also tried with this function from github
pause(2.5); %i included a delay in case the subplot required additional time to generate but it did not solve the issue
end
Above is the small conditional loop that is entered if "Continue & Save" is selected. If only "Continue" is selected, no extra code is required as it is simply at the end of the loop and therefore the next iteration begins automatically.
I apologise for not attaching some reproducible code, but it is important to note that the subplots are correctly generated if only "Continue" is selected.
Furthermore, I have cleared the figures and even closed the windows between each iteration to attempt to detach any over-hanging effects, yet the problem still persists.
If anyone has any advice for how to fix this issue I would be very grateful.
My appreciation in advance for my first posted question,
Ruairi
To summarise: saveas changes the behaviour of subplot in a loop
0 件のコメント
採用された回答
Subhadeep Koley
2020 年 5 月 27 日
Ruairi, refer the demo example below,
close all; clear; clc;
for idx = 1:3
% Generating random image for demo
img = rand(10);
figure;
subplot(1, 3, 1)
imagesc(img)
axis image off
subplot(1, 3, 2)
imagesc(img)
axis image off
subplot(1, 3, 3)
imagesc(img)
axis image off
% Dialog box
answer = questdlg('Choose an option?', ...
'Options', 'Continue & Save', 'Continue', 'Continue');
% Handle response
switch answer
case 'Continue & Save'
saveas(gcf, ['figure_',num2str(idx),'.png']);
close;
case 'Continue'
close;
continue;
end
end
Hope this helps!
4 件のコメント
Subhadeep Koley
2020 年 5 月 28 日
In my end both 'switch case' and 'if-else' worked as expected. Refer the code below,
close all; clear; clc;
for idx = 1:3
% Generating random image for demo
img = rand(10);
figure;
subplot(1, 3, 1)
imagesc(img)
axis image off
subplot(1, 3, 2)
imagesc(img)
axis image off
subplot(1, 3, 3)
imagesc(img)
axis image off
% Dialog box
answer = questdlg('Choose an option', ...
'Options', 'Continue & Save', 'Continue', 'Continue');
% Handle response using switch case
% switch answer
% case 'Continue & Save'
% saveas(gcf, ['figure_',num2str(idx), '.png']);
% close;
% case 'Continue'
% close;
% continue;
% end
% Handle response using if-else
if strcmpi(answer, 'Continue & Save')
saveas(gcf, ['figure_', num2str(idx), '.png']);
close;
else
close;
continue;
end
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Printing and Saving についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!