Plotting on axes in a GUI after running the GUI's callback from a different script

Hi,
I have created a GUI called testGUI to demonstrate my issue. It has two axes (axes1, axes2) and a pushbutton. The pushbutton plots one set of data in each set of axes. The callback for the pushbutton is :
function plot_button_Callback(hObject, eventdata, handles)
axes(handles.axes1)
scatter([1,2],[1,2],'og')
axes(handles.axes2)
scatter([30,40],[43,44],'xr')
This works fine when I use the mouse to click the pushbutton. However, I would like to run the GUI from another script.
fig_handle = testGUI;
handles = guidata(fig_handle);
testGUI('plot_button_Callback',fig_handle,[],handles)
Running the axes(handles.axes1) activates the testGUI figure but then opens a new figure and set of axes, plotting the data on that figure instead. Regardless of how I try to make the GUI figure current, if I run the plot_button_Callback from another script, it will open a new window. I can't find any differences in the figure properties. If I press the pushbutton with the mouse after opening the GUI from the separate script, everything plots as expected.
Any insight would be appreciated!

3 Comments

You should never be trying to call a callback of a GUI component from somewhere external, it just doesn't make sense.
If you want to run the same code from two different places then just put it in a normal function in its own file and pass in the arguments you need.
Processing that is to be displayed in a GUI makes most sense to be run within the GUI also. Fishing handles of a GUI out to an external place and using them there is not a good idea in any context even though I see a lot of people do it.
You should, however, get used to plotting like this:
scatter(handles.axes1, [1,2],[1,2],'og')
scatter(handles.axes2, [30,40],[43,44],'xr')
to explicitly tell it which axes to plot on rather than bringing each axes to focus in turn.
Fair enough. Thanks for the tips! I was trying to save myself rewriting thousands of lines of code and maintaining the ability to visualise it in the GUI at any point but I guess it's the less correct way to do it.
It depends what your GUI looks like, but you shouldn't need to rewrite thousands of lines of code, mostly just re-house functionality and link it up differently. If you are running things from external to your GUI wouldn't that mean that everything else on the GUI is out of sync anyway if you just update the axes?

Sign in to comment.

Answers (1)

Why do you want to run the callback from a script instead of using the pushbutton?
testGUI('plot_button_Callback',fig_handle,[],handles)
This line of code is actually calling the testGUI function, not just the callback, which is why a new testGUI figure is being opened.

1 Comment

I have another, way more complex GUI that I built to process data. It was much easier to use a GUI to refine my process visually. However, I don't want to try and take all of the code out of the GUI to process all the data in one go. It's not really necessary that it plots it since I'm trying to automate the process, but I'm worried that I'm passing arguments incorrectly and this issue is a product of that.
There isn't another testGUI figure being opened. It's opening a figure called 'Figure 1' for the sole purposes of plotting the data. There's still only one instance of testGUI being run as far as I can tell. If it is calling a new instance of the GUI, is it possible to call the Callbacks without a perceived new instance of it being loaded?

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Asked:

on 27 Sep 2016

Commented:

on 28 Sep 2016

Community Treasure Hunt

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

Start Hunting!