필터 지우기
필터 지우기

GUIDE Callbacks using function handles or strings

조회 수: 1 (최근 30일)
Jette
Jette 2012년 11월 8일
Hello,
I'm a quite experienced GUI programmer and usually program GUIs manually without using GUIDE. However, now I struggle with a GUI which was developped by someone else using GUIDE.
I would like to define a callback function for a GUI element of the GUIDE GUI mygui.m/mygui.fig without using the standard GUIDE callback which is defined in mygui.m. Currently, the callback function defined in mygui.m just calls another function which is saved in a seperate file cb.m because it is quite long. To improve maintenance of the software, I would find it much clearer if I could directly define the callback function to be cb.m whithout having to go via a subfunction of mygui.m.
It tried to change the callback property in GUIDE like
cb(hObject,eventdata)
@(hObject,eventdata) cb(hObject,eventdata)
but I could not get it running. Now I wonder if it is at all possible to avoid the "normal" GUIDE callback in mygui.m? If there is another possibiliy - how to do it?
I'm using MATLAB R2010b.

채택된 답변

Jette
Jette 2012년 11월 15일
By now, I found that defining the callback property in GUIDE to be
cb(gco)
works. The variable hObject seems not to be known when I define callback functions myself although hObject seems to be known for callback functions generated by GUIDE itself.
With this solution I don't even need the second argument which callback functions usually must have.

추가 답변 (1개)

Akiva Gordon
Akiva Gordon 2012년 11월 15일
In the GUI's OpeningFcn, you can try:
set(handles.object,'Callback',@cb)
and just replace "object" with the actual tag of that object for which you want the callback to be cb.
If cb has some input arguments, be sure to wrap the 'Callback' value in curly braces like this:
set(handles.object,'Callback',{@cb,input1,input2,...})
  댓글 수: 1
Jette
Jette 2012년 11월 15일
OK. This might work but I find it unsatisfying:
  • You probably have to keep the original Callback function because otherwise the interplay between GUIDE fig and mat file does not work anymore. Alternativly one could delete the Callback in the fig file at all.
  • Software maintenance becomes not clearer with this solution because one would have 2 places where Callback functions are defined.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by