GUI: How to update the handles when calling a function Callback inside another Callback?
조회 수: 16 (최근 30일)
이전 댓글 표시
Hello,
I am having some problems with a Matlab GUI, basically the problema appears when I try to call a Callback function from another Callback function, in the sense that my handles variable does not get updated once I get out from the second callback.
To be more specific: I have a Callback function (from a push button) and I want it to execute also the code from a different push button, so I call it inside this function in the way:
CalledComponent_Callback(handles.CalledComponent, [], handles)
Inside this function I am updating the handles as:
guidata(hObject, handles);
But once I get out the variables included to handles in the CalledComponent are no longer available.
I have been reading some answers to similar questions, but I just found cases for a user function, not another particular Callback function, so it does not solve my problema.
I would be grateful for any help. Thanks,
Ana
댓글 수: 0
채택된 답변
Sara
2014년 3월 20일
If I understand right, now you have something like:
function callback_1()
...code 1
callback_2
guidata(hObject, handles);
function callback_2()
...code 2
guidata(hObject, handles);
To solve your problem, take the code from the second callback and put it in a function. Return the handle if the function modifies it:
function callback_1()
...code 1
handles = new_funct(handles)
guidata(hObject, handles);
function callback_2()
handles = new_funct(handles)
guidata(hObject, handles);
function handles = new_funct(handles)
...code 2
추가 답변 (4개)
Philippe
2014년 9월 4일
Hi,
I'm not sure if this thread is still relevant but here is a simple solution that seems to work for me (this is for 2012b btw). In this example, main_callback is the callback of the "main" button which starts two secondary callbacks, each linked to their own pushbutton. Since I don't want to add extra user defined functions I just output the handles as if it were a regular variable from the secondary callback functions. This works well if the data you want to pass between the main callback and the secondary callbacks is regular data (kind of using handles like a global structure).
function main_Callback(hObject, eventdata, handles)
handles = choosefiles_Callback(hObject, eventdata, handles);
handles = readfiles_Callback(hObject, eventdata, handles);
guidata(hObject, handles);
end
function handles = choosefiles_Callback(hObject, eventdata, handles)
handles.chosenfile = 'xyz';
guidata(hObject, handles);
end
function handles = readfiles_Callback(hObject, eventdata, handles)
handles.filecontent=xlsread(handles.chosenfile,...);
guidata(hObject, handles);
end
댓글 수: 1
nl2605
2014년 3월 20일
Hey Ana,
http://www.mathworks.de/de/help/matlab/creating_guis/share-data-among-callbacks.html i think this would be the best method to share data for you. Check it. Using setappdata and getappdata.
댓글 수: 3
nl2605
2014년 3월 21일
I infact tried setappdata and getappdata on my gui today. It worked good. I think this would be a better method but then whatever suits you. :)
Aravinda
2014년 10월 23일
I wanted to load a music file from a pushbutton(music), but play it from a different pushbutton(play) with a slider(volume) for volume control. I could do this successfully which required the scaled music file in music to be available in play. I did the following:
In the function-opening_function,I initialised 'asam'as handles.asam=0;
I had the following code for the 'music' call back: clc k=get(handles.volume,'value'); a=wavread('handel.wav'); b=k*a; handles.asam=handles.asam+b; guidata(hObject, handles);
I had the following code for the 'play' callback d=handles.asam; wavplay(d)
On running the GUI and operating music,volume and play successively, the snippet is heard in proportion to the slider setting.(handel.wav is a standard audio snippet)
By defining required variables in addition to asam, they can be made global within the GUI.
May be the approach helps you solve yours.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!