How to create uicontrols with callbacks in one m-file and call these callbacks in another m-files?
3 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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);
More Answers (2)
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
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).
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:
- http://www.mathworks.com/company/newsletters/articles/writing-apps-in-matlab.html
- http://blogs.mathworks.com/steve/2013/02/21/revisiting-dctdemo-part-2/ All four parts
Once you figure out the syntax, all of a sudden using multiple files and utilizing methods becomes really easy.
3 Comments
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.
See Also
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!