MATLAB Answers

Saveas function changes behaviour of Subplot in subsequent iterations of a loop

1 view (last 30 days)
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

Sign in to comment.

Accepted Answer

Subhadeep Koley
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

Show 1 older comment
Ruairi Gallagher
Ruairi Gallagher on 27 May 2020
Hi Walter, thanks for your input. In this case, the saveas function was used in an identical way as the "problematic code". The only thing that changed was the method of handling the dialogbox.
Even if I replace "gcf" with the specific figure (in this case calling figure(1) inside the save function), the problem remains solved. And furthermore, as part of my attempt to solve the issue, I used "close all" before the next iteration, therby (assumedly) resetting all graphical settings that may have occurred during the loop.
Perhaps I have misunderstood you, and either way, thanks for taking the time to help!
Ruairi
Subhadeep Koley
Subhadeep Koley on 28 May 2020
@ Ruairi Gallagher "Would it be possible for you to explain why?..."
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

Sign in to comment.

More Answers (0)


Translated by