MATLAB: How to use local functions in GUI in order to organize callbacks in mutliple .m files

callbacksguigui_mainfcnguideuicontrol

Hi everyone
My GUI main file becomes bigger and bigger. So, I want to organize it in multiple .m files. My goal is to have a structure like this:
  • main.m => GUIDE related functions
  • SubCallbacks_Cntrl.m => contains Callbacks for "Control action" such as Pushbuttons, checkboxes, popup menues,…
  • SubCallbacks_DataMngmt.m => contains Callbacks for data processing tasks (listboxes, tables,…)
I do know that my Sub_xxx.m files have to pass all function handles to the main.m file in order to make them available. My understanding so far for Sub_CntrlCallbacks.m:
function callbacks = SubCallbacks_Cntrl
callbacks = struct('pushbutton1' , @pushbutton1_Callback , 'checkbox1' , @checkbox1_Callback)
end
function pushbutton1_Callback(hObject, evt, handles)
% Code

end
function checkbox1_Callback(hObject, evt, handles)
% Code
end
In my main.m file I also know how to call such local functions manually with:
SubCallbacks = SubCallbacks_Cntrl
SubCallbacks.pushbutton1_Callback(hObject, evt, handles)
and I could do this at any line of code in my main.m file. But I want to dynamically link this to the GUIDE framework, using the gui_mainfcn(). But I have no idea how!
The only AddOn I made in the main.m so far is the call of my SubCallbacks in the OpeningFcn():
function varargout = main(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @HoloGUI_OpeningFcn, ...
'gui_OutputFcn', @HoloGUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
%- End initialization code - DO NOT EDIT -%
%=========================================================================%
% --- Executes just before main is made visible.
function main_OpeningFcn(hObject, eventdata, handles, varargin)
handles.SubCallbacks = SubCallbacks_Cntrl
guidata(hObject, handles);
But what is the way to work with the gui_mainfcn()?

Best Answer

function main_OpeningFcn(hObject, eventdata, handles, varargin)
SubCallbacks = SubCallbacks_Cntrl;
set(handles.checkbox1, 'Callback', SubCallbacks.checkbox1_Callback)
set(handles.pushbutton1, 'Callback', SubCallbacks.pushbutton1_Callback)
Then add this in each callback to obtain the current value of the handles struct:
function checkbox1_Callback(hObject, evt)
handles = guidata(hObject);
...
Alternatively you can use a wrapper function: Define the same function for all callbacks and branch by using an additional argument:
function SubCallbacks_Cntrl(hObject, EventData, ObjectName)
handles = guidata(hObject)
switch ObjectName
case 'pushbutton1'
pushbutton1_Callback(hObject, EventData, handles)
case 'checkbox1'
checkbox1_Callback(hObject, EventData, handles)
...
end
And the single callbacks are following. You could use the tag of the object also:
switch hObject.Tag
...