How do I make my GUI plot into an axes within the GUI figure rather than inside of a new figure in MATLAB?

13 views (last 30 days)
I get a new axes or figure whenever I try to put a plot into an existing axes in my GUI, regardless of whether the plotting command is issued from the GUI's callbacks or the MATLAB Command Window.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This change has been incorporated into the documentation in MATLAB 7.1 (R14SP3). For previous releases, read below for any additional information:
There are 2 factors in each of these situations:
1. The availability (HandleVisibility) of the axes to be plotted in.
2. The selection of the current axes (where the plot will be created).
An explanation of how to set the properties of the figure/axes properly and how to make sure that the proper axes are the current set of axes is listed for these situations below. Note that there are also other methods for indicating in which axes you want to plot:
1. Certain plotting routines allow you to specify the axes to plot in by including the Parent property-name/property-value pair as an input, such as the low-level plotting commands like LINE and SURFACE.
2. Other routines won't allow you to specify the Parent axes of the graphics objects as an input, i.e. high level plotting commands like STEM. You can, however, set the Parent of the graphics objects after they are created:
p=stem(1:10);
set(p,'Parent', axes_handle)
(Where axes_handle is the handle of the axes in which you want the object to appear.) One problem with this method is that it may cause the creation of a new figure or set of axes, which will still exist after the object's Parent is changed. In this case, you can use the DELETE function to remove the extra figure/axes.
Situation #1: Inside the GUI's callbacks
**********************************************
Use the AXES command to select the set of axes that you want to plot in, before executing the plotting command. Since the axes should have already been added to the GUI in GUIDE, a handle for it should have been included in the 'handles' structure of the GUI. If the Tag property value for the axes you want to plot to is 'axes1', then its handle is stored as 'handles.axes1'. Use the following to make it the current set of axes for plotting.
axes(handles.axes1)
You will also need to make sure that the figure's and axes' handles are 'visible' to the Callback functions. This will make them available for plotting commands. Set their HandleVisibility properties to 'callback'. You should also check the GUI's Command-line accessibility option:
1. Choose GUI Options (R13) or Application Options (R12.1/R12.0) from the Tools menu of GUIDE.
2. In the Command-line accessiblity pulldown menu, select Callback.
3. Click OK.
Situation #2: From the Command Window
************************************************
By default, GUI's have their Handle Visibility set to Callback, so they won't be affected by commands from the MATLAB Command Window. There are 2 ways to work around this:
1. Setting the figure's and axes' HandleVisibility property to 'on'.
2. Setting the root's ShowHiddenHandles property to 'on'.
The first method will affect only that GUI. It will make the GUI accessible from the command line, like standard figures. The second method will apply to all GUIs/figures that are open, in which case there may be some confusion regarding to which axes the plotting commands will be applied. In either case you should use the AXES command to set the current axes. If you have multiple axes, you may need to use another command to determine the handle for the axes that you want to plot into. You can use the FINDOBJ function to locate a particular object whose properties match certain property/value pairs. For example the following will return a variable 'h' that contains the handle(s) for any object(s) that are a Type 'axes' and have a Tag 'axes1'.
h=findobj('Type','axes','Tag','axes1');
It is possible to have more than one object that matches these properties, so you should make sure that you either set distinct properties (Tag is useful for this purpose, i.e. 'naming' your objects) for all of your axes, or use enough property/value pairs to narrow the possibilities down to one object. Then use AXES to select the axes, before issuing your plotting command:
axes(h)

More Answers (0)

Categories

Find more on Graphics Object Identification in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!