Non-deterministic behavior with multiple figures

35 views (last 30 days)
If I try to call multiple figures in a script and then update them afterwards by adding a title or plotting on top of them I get weird seemingly random behavior. For example:
figure(1)
imshow(myimage, [])
figure(2)
imshow(myimage2, [])
hold on
plot(x,y)
When I run this the plot after the `hold on` shows up on top of figure(1). I am running Matlab on Ubuntu, my office mate running Matlab on OSX tested my code and on his machine it runs the way you would expect, plotting ontop of figure 2.
I've also experienced similar issues when trying to do something like:
figure(1)
imagesc(myheatmap)
title('Title 1')
figure(2)
imagesc(myheatmap)
title('Title 2')
In this example the titles may or may not be swapped. I don't understand how to control this behavior to get the desired output but it really ruins some scripts for me. Any help is appreciated.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Dec 2016
The behavior is deterministic, but the behavior depends upon more than is obvious.
Any time you execute a graphic command without explicitly specifying what figure or axes it is to apply to, then it applies to the "current" figure or "current" axes.
Creating a figure in the form you use is defined to make the figure current.
The imagesc() that you do in the instruction after that will work on the current axes. If the figure(1) call ended up creating the figure, and if figure 1 is still current, then there is no current axes and one will automatically be created and made current and then drawn in by the imagesc()
A key point in that description is "and if figure 1 is still current". If there has been no keyboard or mouse or debugger interaction, no graphic callbacks (calling figure() is specifically defined as one of the occasions on which queued graphics callbacks might be run), no timer callbacks -- if "nothing happened" between the time the figure was activated and the time the imagesc() is run, then figure 1 will still be the current figure. But a timer or graphics callback might have changed that.
More particularly, if you clicked your mouse for any reason after the figure was created, then the current figure might have changed. If your window manager asks you specifically position each window, then you would have clicked and MATLAB's idea of the current figure might have changed. If you hit a breakpoint and you clicked to bring up the command window, or if you moved a window in order to show the command window, then MATLAB's idea of the current figure would have changed.
What is recommended to avoid this problem is to always, in every case, specify the object that the graphics command is to happen against. You might find this referred to as "always parenting" your graphics. For example,
fig1 = figure(1);
ax1 = axes('Parent', fig1); %do not allow the axes to be automatically created
imshow(myimage, [], 'Parent', ax1); %do not assume the axes is still the active one
fig2 = figure(2);
ax2 = axes('Parent', fig2); %do not allow the axes to be automatically created
imshow(myimage2, [], 'Parent', ax2); %do not assume the axes is still the active one
hold(ax2, 'on'); %do not assume the axis is still the current one
plot(ax2, x, y); %do not assume the axis is still the current one
You might have noticed here that some commands allow an axes to be passed as the first parameter. Others require the 'Parent' option. Some commands support both syntaxes; the ones that support passing in an axes almost always allow 'Parent' instead.
If you encounter a graphics creation command that does not permit an axes or a 'Parent' to be passed, then the implication is typically that it is going to create a new figure of its own. However, sometimes the implication is that it is going to more or less erase any axes in the current figure and draw something. (Some of the video players do that :( )
  1 Comment
Michael Vaiana
Michael Vaiana on 16 Dec 2016
Ok thank you so much for the detailed response, that is exactly what I was looking for!

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!