How to create uicontrols with callbacks in one m-file and call these callbacks in another m-files?
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (2)
Image Analyst
el 6 de Sept. de 2013
0 votos
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 comentarios
Image Analyst
el 6 de Sept. de 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é
el 7 de Sept. de 2013
Sean de Wolski
el 6 de Sept. de 2013
0 votos
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 comentarios
Walter Roberson
el 6 de Sept. de 2013
... but watch out for the speed differences :(
Sean de Wolski
el 6 de Sept. de 2013
The speed differences should be negligible for anything involving human interaction where human perception and motion will be the rate-limiter.
André
el 7 de Sept. de 2013
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!