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;
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;
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');
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!
Best Answer