Link two pushbutton_Callback (hObject, eventdata, handles)

10 views (last 30 days)
Hello everyone!
I need to link two different Pushbotton_Calback(hObject, eventdata, handles) in two different file as below:
in myGUI.m:
function varargout = myGUI(varargin)
% some code here and then:
function pushbutton_load_Callback(hObject, eventdata, handles)
% some code here
and
in search.m
function pushbutton_ok_Callback(hObject, eventdata, handles)
% some code here
what i want is to link pushbutton_ok with pushbutton_load as i push pushbutton_ok, in search.m then pushbutton_load in myGUI.m is also be pushed.
I would be happy if anyone could help!
Thank you!

Accepted Answer

Cam Salzberger
Cam Salzberger on 18 Oct 2017
Hello Behzad,
If I'm understanding you correctly, you have two separate GUIs, each with their own button. When you push the "OK" button in the "Search" GUI, you want this to also execute the same code that would be run when you hit the "Load" button in the "myGUI" GUI.
One way to do this would be to simply have a separate M-file that contains the shared code you want to run. Both of the callbacks would simply call that separate function, and you wouldn't have to worry about giving access to methods of one GUI from the other. This method exposes more files/code to users though, and isn't very scalable.
Another possibility might be, if you create/open the "Search" GUI from "myGUI", you could pass the handle to the function you want to call in the constructor. Then, in the opening function of "Search", you can save that function handle to the "handles" guidata, and call it within the pushbutton callback.
Another way to do this would be to implement some kind of class-based system, where you make the shared code accessible to both classes (either making that method public, or using meta-class access specification). This would actually be easiest to do if you were using App Designer rather than GUIDE, since the GUIs are created as a class to start with. If you haven't gotten too far into your GUI development, consider it as an alternative.
-Cam
  3 Comments
Cam Salzberger
Cam Salzberger on 19 Oct 2017
Hello Behzad,
Here's a brief example. It doesn't follow exactly your use-case, but it should help to illustrate the workflow.
In MainGui, which has two buttons, one to open SecondGui, the other to take your desired action:
% --- Executes on button press in Button_Open2ndGui.
function Button_Open2ndGui_Callback(hObject, eventdata, handles)
hDoSomething = @DoSomething;
SecondGui(hDoSomething)
% --- Executes on button press in Button_DoesSomething.
function Button_DoesSomething_Callback(hObject, eventdata, handles)
DoSomething(5)
% Helper function
function DoSomething(x)
disp(x)
In SecondGui, which has one button to take the desired action:
% --- Executes just before SecondGui is made visible.
function SecondGui_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for SecondGui
handles.output = hObject;
if numel(varargin) > 0
hFuncToCall = varargin{1};
handles.functionToCall = hFuncToCall;
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in Button_Ok.
function Button_Ok_Callback(hObject, eventdata, handles)
if isfield(handles, 'functionToCall')
handles.functionToCall(5)
end
Again, these are just examples, and you can customize the code however you want.
-Cam

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!