Saveas function changes behaviour of Subplot in subsequent iterations of a loop
3 views (last 30 days)
Show older comments
Ruairi Gallagher
on 27 May 2020
Commented: Subhadeep Koley
on 28 May 2020
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 Comments
Accepted Answer
Subhadeep Koley
on 27 May 2020
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 Comments
Subhadeep Koley
on 28 May 2020
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
More Answers (0)
See Also
Categories
Find more on Printing and Saving 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!