vmontazeri
vmontazeri

Reputation: 423

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

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.

Upvotes: 4

Views: 2216

Answers (1)

il_raffa
il_raffa

Reputation: 5175

A possible solution can be the following.

Let's assume you have GUI 1 defined as follows:

  • GUI filename (the name of both the .m and .fig files) = master_gui
  • figure tag property defined as "figure1"
  • figure handlevisibility property set on
  • callback to be called from GUI 2: plot_something_Callback

Then from, say, a pushbutton in GUI 2 you want to call plot_something_Callback defined in GUI 1

So, in the GUI 2 pushbutton callback you can code:

% Get the GUI 1 obj 
g_m=findobj('tag','figure1')
% Get GUI handles
gd_m=guidata(g_m);
% Call GUI 1 callback from GUI 2 callback
% master_gui('plot_something_Callback',gd_m.plot_something,[],g_m)
master_gui('plot_something_Callback',gd_m.plot_something,[],gd_m)

Notice, GUI 1 shall be open.

CODE MODIFIED The last parameter in the call of GUI 1 callback is gd_m that is the GUI 1 handles

Sorry about the bug (the code I've originally posted worked because GUI 1 handles were not used in GUI 1 callback)

Hope this helps.

Upvotes: 4

Related Questions