How to create uicontrols with callbacks in one m-file and call these callbacks in another m-files?

3 views (last 30 days)
Hello,
My gui is in the main m-file and getting larger, so I would like to put the gui initialization in a separate m-file, in a sub m-file. But when I call in the main m-file the callbacks of the uicontrols of the initialization, I get errors like this:
>> test_gui
Undefined function 'pushbutton1_callback' for input arguments of type 'double'.
Error while evaluating uicontrol Callback
So my question is: How to create uicontrols with callbacks in a sub m-file and call their callbacks in the main m-files? Here is a simple code:
Here is the code of the main m-file:
% Main m-file.
function test_gui()
handles = initgui;
% Should execute when pushbutton1 is pressed. But here matlab's
% codeanalyzer tells me: "The function 'pushbutton1_callback' might be unused."
function pushbutton1_callback(handles)
get(handles.pushbutton1, 'String');
% do more …
Here is the code of the sub m-file:
% Sub m-file.
function handles = initgui()
% New figure.
handles.f = figure;
% New pushbutton with callback definition.
handles.pb = uicontrol(handles.f, 'Units', 'normalized', ...
'Position', [0.3 0.3 0.3 0.3], ...
'Style', 'Pushbutton', ...
'Tag', 'pushbutton1', ...
'String', 'Push me!', ...
'Callback', @pushbutton1_callback);
% Save handles in figure with handle f.
guidata(handles.f, handles);
end

Accepted Answer

Walter Roberson
Walter Roberson on 6 Sep 2013
First off, you need
function pushbutton1_callback(hObject, event, handles) %change signature
Secondly, when you define multiple functions in the same file, the extra functions are "file scope", only visible to the text of other functions in the same file, other than when you specifically transfer the function handles elsewhere. So in function test_gui(), code,
init_gui(@pushbutton1_callback)
and then in init_gui.m,
function handles = initgui(button_callback)
and change
'Callback', @pushbutton1_callback);
to
'Callback', button_callback);
  1 Comment
André
André on 6 Sep 2013
Hi Walter,
your answer was very helpful, thank you! Here my code which works now:
% Main m-file.
function test_gui()
handles = initgui(@pushbutton1_callback);
% --- Executes when pushbutton1 is pushed.
function pushbutton1_callback(hObject, event, handles)
get(hObject, 'String');
% do more …
% Sub m-file.
function handles = initgui(button_callback)
% New figure.
handles.f = figure;
% New pushbutton with callback definition.
handles.pb = uicontrol(handles.f, 'Units', 'normalized', ...
'Position', [0.3 0.3 0.3 0.3], ...
'Style', 'Pushbutton', ...
'Tag', 'pushbutton1', ...
'String', 'Push me!', ...
'Callback', button_callback);
% Save handles in figure with handle f.
guidata(handles.f, handles);
end

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 6 Sep 2013
You need to pass the handles structure of your main GUI to the called GUIs, as well as return it like you're already doing. Then the called GUIs will have the handle ID numbers of the graphical elements on the main GUI that they need to control. Basically, you're returning a variable called handles that is a totally separate and new handles (created in the sub gui) that is not related at all to the main gui's handles structure.
  3 Comments
Image Analyst
Image Analyst on 6 Sep 2013
Are you using GUIDE? It doesn't look like it. It seems like you're wanting to "roll your own." I think that's tedious, especially if you have dozens of controls on your GUI, but if you want to do that, please study Matt Fig's file exchange submission of 41 examples where he shows you how to do it manually like this. Well anyway, from your comment to Walter, it looks like now you have a solution. By the way, the handles structure of the main gui is the variable you called "handles". You passed it into initgui and then you can either pass it back out, or call guidata (which is what you did).
André
André on 7 Sep 2013
Hi,
no, I'm not using GUIDE. The reason for this decision is to keep the possibilities of using interesting programs/files of fex. I think it could be very difficult to integrate these programs in a GUIDE created GUI.
Matts 41 examples are exactly the kind of introducing I was looking for, thanks! Especially the question 21, 26, 34 and 47 are of interest for me.

Sign in to comment.


Sean de Wolski
Sean de Wolski on 6 Sep 2013
There's a bit of a learning curve but if your application is going to be complex or you plan on making other applications in the future, I suggest biting the bullet and going with object oriented programming.
Here are some resources:
Once you figure out the syntax, all of a sudden using multiple files and utilizing methods becomes really easy.
  3 Comments
Sean de Wolski
Sean de Wolski on 6 Sep 2013
The speed differences should be negligible for anything involving human interaction where human perception and motion will be the rate-limiter.
André
André on 7 Sep 2013
Hi Sean,
the advantage of using APPS seems to be the easy way to store and access shared data in different parts of the APP and in my case in the GUI. Thanks for the links! I think the time is come to dare of using CLASSES and METHODS :-)

Sign in to comment.

Categories

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