Keep figures invisible until for loop is over to reduce time duration

From numerous data files I extract a column and plot all those columns into one graph. I do this by looping through them all in a for-loop. Then I make, say, 4 graphs likes this for different columns in those data files. Each graph is therefore appended an extra curve in each round in the for-loop.
The code is something like this:
for j=1:noOfFiles
figure(1); hold on; plot(A,B); hold off
figure(2); hold on; plot(C,D); hold off
figure(3); hold on; plot(A,D); hold off
figure(4); hold on; plot(A,F); hold off
end
Now, the issue is that when running the script from the command window in MatLab, each of the 4 figures open in a new window and I can visually see the curves being added at high speed. But for MatLab to show this visually, a lot of unnecessary time is wasted. There can be a quite large amount of curves to be added to each graph, so time reduction is my focus now.
I would like MatLab to only focus on running the loop and not on graphically displaying the graphs along the way, since this is extra time consuming. Maybe by making them invisible during the run?
What is the smartest method for obtaining this?

1 Comment

Can you attach the code? You can probably adjust the code to add multiple lines in one plot command.

Sign in to comment.

 Accepted Answer

fig1 = figure(1, 'Visible', 'off');
ax1 = axes('Parent', fig1);
hold(ax1, 'on');
fig2 = figure(2, 'Visible', 'off');
ax2 = axes('Parent', fig2);
hold(ax2, 'on');
fig3 = figure(3, 'Visible', 'off');
ax3 = axes('Parent', fig3);
hold(ax3, 'on');
fig4 = figure(4, 'Visible', 'off');
ax4 = axes('Parent', fig4);
hold(ax4, 'on');
for j = 1 : noOfFiles
plot(ax1, A, B);
plot(ax2, C, D);
plot(ax3, A, D);
plot(ax4, A, F);
end
hold(ax1, 'off');
hold(ax2, 'off');
hold(ax3, 'off');
hold(ax4, 'off');
set([fig1, fig2, fig3, fig4], 'Visible', 'on');

6 Comments

Thank you for the answer. With such a "handle" for each figure, how can I furthermore attach plot details to the graphs subsequently and also save them? At the moment I do the following:
figure(1)
title('Status')
set(gca, 'XScale', 'log')
xlabel('Time in seconds')
text(0.5,0.5,'A small message','Units','normalized','FontSize', 8)
xlim([0 360])
figure(2)
title('Status')
set(gca, 'XScale', 'log')
xlabel('Time in seconds')
text(0.5,0.5,'A small message','Units','normalized','FontSize', 8)
xlim([0 360])
...
figure(1); savefig([savePath 'graph1.fig'])
saveas(figure(1),[savePath 'graph1.png'])
figure(2); savefig([savePath 'graph2.fig'])
saveas(figure(2),[savePath 'graph2.png'])
...
Also, may I ask what the necessity of the axes parameters are? Can MatLab not assign a plot to a figure with plot(fig1, A, B)? Is plot(ax1, A, B) with the axes parameter necessary?
title(ax1, 'Status');
set(ax1, 'XScale', 'log')
xlabel(ax1,'Time in seconds')
text(ax1, 0.5, 0.5, 'A small message', 'Units', 'normalized', 'FontSize', 8);
xlim(ax, [0 360]);
@Steeven:
  1. you should replace gca with the axes handle
  2. graphics functions usually support an (optional) input for the parent object handle. I just checked the title, xlabel, text, and xlim documentation and all of them support a handle input, so you can simply supply the axes handle to the function: read their help pages to know more.
I would recommend that you always obtain and use explicit graphics handles for all graphics objects: figures, axes, lines, patches, etc, that you use. Do not rely on the current figure/axes/... as being the correct one.
Figures often have multiple axes, so plot(fig1, A, B) is typically not enough and MATLAB did not choose to implement it. Colorbars, legends, and annotation() objects are all implemented with axes.
If you were desperate to save a line at the expense of considerable added confusion, in each place that I passed ax1, you could replace that with findobj(fig1, 'type', 'axes') such as
plot(findobj(fig1,'type','axes'), A, B);
title(findobj(fig1,'type',axes'), 'Status');
Thank you both for you suggestions.
@WalterRoberson, your code example works if I remove the numeric numbering of the figures, meaning from this:
fig1 = figure(1, 'Visible', 'off');
to this:
fig1 = figure('Visible', 'off');
Thanks again for the help.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties 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!