How to plot figures at the end of the for loop MATLAB?

67 Views Asked by At

Hello I have the following piece of code which plots my results:

h1 = figure;
h2 = figure;

for i = 1:5
 figure(h1);
 set(h1, 'Visible', 'off');
 semilogy(0:iter_jac ,resvec_jac,'-*');
 hold on;

 figure(h2);
 set(h2, 'Visible', 'off');
 subplot(3,2,mesh);
plot(trace(:,2),trace_sol(:,1),trace(:,2),trace_sol(:,2),trace(:,2),trace_sol(:,3),trace(:,2),trace_sol(:,4));
 xlabel('arc length');
 ylabel('temperature');
 legend('t = 2.5','t = 5.0','t = 7.5','t = 10');
 title(sprintf('Mesh %d', mesh));
end

What this code is doing is basically plotting all the semilogy in one plot while the trace plots in on figure with different subplots. However the problem with this code is that it shows me the figure at every iteration of the for loop (so I see the new results being plotted) even if I set both figures invisible (they still appear for a second). How can I ONLY show the plots once the for loop has ended?

1

There are 1 best solutions below

0
Cris Luengo On BEST ANSWER

Calling figure(h1) causes the figure to become visible.

The best way around this is to explicitly use figure and axes handles to do all your work (instead of relying on the current figure/axes and changing those with figure or subplot). Functions that don't directly take a handle as input can take a name-value pair to set the property 'parent' to a handle.

h1 = figure('Visible', 'off');
h2 = figure('Visible', 'off');
ax1 = axes(h1);
ax1.NextPlot = 'add';  % equivalent to 'hold on'

for i = 1:5
 semilogy(ax1, 0:10, rand(1,11) ,'-*');

 ax2 = subplot(3, 2, i, 'parent', h2);
 plot(ax2, 1, 4, 'o');
 xlabel(ax2, 'arc length');
 ylabel(ax2, 'temperature');
 title(ax2, sprintf('Mesh %d', i));
 legend(ax2, 't = 2.5');

 % Just to demonstrate that figures are not shown until after the loop:
 pause(1);
end

h1.Visible = 'on';
h2.Visible = 'on';

[Note I've modified the code also to plot random data, this is just an illustration.]