How to call a callback function in a MATLAB gui from another gui?

16 views (last 30 days)
Hi All, I have two GUIs, made with GUIDE. I'm trying to call a pushbutton callback function in GUI1 from GUI2. Here is the code in GUI2
set(0,'showHiddenHandles','on');
hfig = gcf;
m_handles = guidata(hfig); % handles in GUI1
set(m_handles.show_status , 'String' , ...
[script sprintf('\n') s_response]); % this line works
set(m_handles.add_note , 'Enable' , 'off'); % this line also works
add_note_Callback(m_handles.add_note, eventdata, m_handles); % but this does not work!
Here is the error I get Undefined function 'add_note_Callback' for input arguments of type 'struct'. any help will be appreciated.

Answers (2)

Adam
Adam on 1 Jul 2015
Edited: Adam on 1 Jul 2015
Factor out your callback function into its own file so you can call it and pass in the arguments.
You cannot see functions externally that are subfunctions of another function (such as a GUIDE GUI)
I would not recommend this method of programming in any way though. Having one GUI accessing the handles of another GUI is very dodgy design even though it is made possible by methods like showing hidden handles.
Unfortunately the alternative that I use is too complicated to explain in a manner for someone to understand who has never done Matlab OOP programming though so what I said first should work for you.
On a very high level though if you work with classes underlying your UIs you can pass these class objects from one UI to another (assuming they are handle-derived classes to pass by reference), attach listeners and then each GUI still controls all its own behaviour instead of GUIs dipping into each other and potentially creating all manner of hard to find bugs.

Ephesians289
Ephesians289 on 2 Nov 2016
Edited: Walter Roberson on 2 Nov 2016
GUI_MFILE('CALLBACK',hObject,eventData,handles,...)
calls the local function named CALLBACK in GUI_MFILE.M with the given input arguments.
  1 Comment
Santhosh
Santhosh on 1 Mar 2017
how to use this command if i want to callback a pushbutton function from outside gui??? i am getting an error message showing
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps 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!