Plotting to an axis-object in a GUI externally

4 views (last 30 days)
Hey there!
I set up a GUI using guide with an axis-object on it (along with many other objects, but they don't matter for this), let's call it ax . I then saved the handle to ax in a global variable:
global plotHandle;
plotHandle = handles.ax;
What I try to do now is to plot something onto it from another script, for example by doing
global plotHandle;
axis(plotHandle);
plot(...);
But when I do so, Matlab opens a new window with the plot in it, rather than plotting it on ax on the GUI. Is there any way I can solve this? Or any other Idea how I could plot on the axis on the GUI from an external script?

Accepted Answer

Geoff Hayes
Geoff Hayes on 28 Aug 2014
Quirin - I guess that you are running the script outside of the GUI (or is it being invoked in response to a push button or other callback within the GUI), else you would have used the handles structure to get the handle to the axes.
I think that one problem is the use of axis versus axes. The former just sets a property in the passed handle, whereas the latter should make, when using axes(plotHandle), plotHandle the current axes upon which to plot some data.
So changing the code to
global plotHandle;
% guard against plotHandle not being set
if ~isempty(plotHandle)
axes(plotHandle);
plot();
end
will set the current axes to plotHandle and will draw the subsequent plot within that axes (of your GUI).
This works fine if (in an example GUI) I press a button which calls the script which has the above code. However, if I invoke the script outside of the GUI, a new window appears with the plotted data. So that doesn't fix the problem.
What you could do instead, is pass the plotHandle as the first input to the plot function
global plotHandle;
% guard against plotHandle not being set
if ~isempty(plotHandle)
plot(plotHandle,...);
end
That seems to work fine because you are explicitly saying on which axes to plot the data. Note that you may want to change the global variable name from plotHandle to axesHandle.
-----------------
An alternative to using a global variable, is to use findobj which would allow you to find the graphics object that you wish to manipulate. In this case, the axes. To do this, in GUIDE, open the Property Inspector for the figure/GUI and set the HandleVisibility to on, then save the changes. This will allow us to search for the GUI (and its children) using findobj.
Now in your script you can do a couple of things. Remove the global variable and do
axesHandle = findobj('Tag','ax');
if ~isempty(axesHandle)
plot(axesHandle,);
end
In the above, we use the tag/name of the axes (similar to what you did when setting the global variable) to query for that object. If we've found it, then we can plot some data.
Or something else
figHandle = findobj('Tag','figure1'); % using the name of your figure/GUI
if ~isempty(figHandle)
axesHandle = findobj(figHandle,'Type','axes');
if ~isempty(axesHandle)
plot(axesHandle,);
end
end
Here, we are querying the figure/GUI for all children of type axes. This works well if there is only one axes though.
Try any of the above, and see what happens!

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output 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!