David Kelley
David Kelley

Reputation: 1448

Set figure to not be target for next plot

I'm working on a custom progress monitor with some graphs. I've noticed that Matlab's waitbar creates a figure with some special properties so that if you do

plot(rand(100,1));
wb = waitbar(0);
plot(rand(100,1));

the second plot ends up replacing the first plot and not in wb. Is there a property I can set so that when I create my progress monitor and then plot something afterwards, the graph doesn't end up in my figure?

To be clear, I'm trying to have

plot(rand(100,1));
temp = MyProgressBar();
plot(rand(100,1));

create a figure for the first plot, create a different figure in the second line, then plot a new graph in the third line.

Upvotes: 1

Views: 107

Answers (1)

gnovice
gnovice

Reputation: 125874

To protect your progress bar figure against subsequent plotting operations, I would set the 'HandleVisibility' property of its axes to 'off'. That should prevent it ever becoming the current axes, thus keeping subsequent plotting commands from modifying or adding to it. It's a good practice for stand-alone figures/GUIs in general that you turn off the handle visibility of all objects (figure, uicontrols, etc.) in this way to insulate them against being modified by outside code. This is almost certainly what is done in the code for waitbar.

As an additional aside, it's good practice to target your plots to a given axes by passing the axes handle as the first argument. You also have to make sure that, if you want new plots to be added to existing plots, you use things like the hold command first. Here's how I'd rework your example, assuming you want the two plots to appear on the same axes:

plot(rand(100,1));  % Creates new figure and axes
hAxes = gca;        % Get the axes handle
hold on;            % Allow subsequent plots to be added

temp = MyProgressBar();

plot(hAxes, rand(100,1));  % Will be added to the first plot axes

Upvotes: 3

Related Questions