What this actually means?????

% Mygui('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in Mygui.M with the given input arguments.
How to use this??? Can this be used to call a sub function(namely a pushbutton) inside a gui from an external function which is a separate m file???

 Accepted Answer

Walter Roberson
Walter Roberson on 2 Mar 2017
I guess you could use it that way. I would not do that myself.
It is mostly for internal use by GUIDE, due to the way it creates wrapper code around callback functions in order to be able to pass the handles structure into each callback.
If you do use it, make sure you pass the correct hObject and event structure required by the target callback.
A cleaner way is to move the code you need activated into a function in a location accessible from both files, and have both places make appropriate calls into the work routine.

7 Comments

In that case i am having a push button which reads and updates the state of buttons in a button group consist of few check boxes, then how will i be able to read the check box state from an external function and update them in the gui??? is that possible???
Yes. Just make sure that the external function is passed either the handles structure or the specific handles it needs to work with.
For example,
function work_routine(handles)
checks = [handles.check1, handles.check2, handles.check3];
nvals = length(checks);
vals = cell2mat(get(checks, 'Value'));
decval = sum(vals .* 2.^(0:nvals-1));
newdecval = mod(decval+1, 2.^nvals);
newchecks = dec2bin(newdecval, nvals) - '0';
for K = 1 : nvals
set(checks(K), 'Value', newchecks(K));
end
This code takes the state of three check boxes as if it were a 3 bit binary number, increments the number by 1 (wrapping from 7 to 0), and sets the boxes to the new states. This illustrates that you can read states, compute with them, and write new states.
Ya this will do i guess, but how can i pass the handles structure to the external function. Do i have to include some code in my main gui m file????
Your callback that is calling the work routine will look something like
function pushbutton1_Callback(hObject, event, handles)
in your main gui.m file if you are using GUIDE.
You can call the above routine by adding the line after that,
work_routine(handles)
Ya it works good, If i call the function using the pushbutton it works perfectly because the handle structure is passed but if i want to trigger the function separately without using the pushbutton how will i be able to pass the handle structure???
If you are using GUIDE, everything useful is within a callback, so you just pass that callback's copy of handles to the work_routine.
But if you just happen to have a different program from which you are invoking a GUIDE-created interface and you want to call the work routine from in there, then you first need to retrieve the handles structure. When you make an outside call to a GUIDE-created interface (by calling the function whose name is the GUI), then the routine returns the GUI figure handle as the return value. You would record that figure handle. From there,
handles = guidata(TheFigureHandle);
work_routine( handles );
Thanks Walter. I got it.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!