필터 지우기
필터 지우기

GUI: How to update the handles when calling a function Callback inside another Callback?

조회 수: 10 (최근 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

채택된 답변

Sara
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
  댓글 수: 1
Ana
Ana 2014년 3월 20일
Yes, something very similar to this is what I have done until now as a temporary solution, but I thought there should be a direct way to call a Callback inside another Callback so that I don't need to do this for every Callback that needs to be called from another Callback.
Ana

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

추가 답변 (4개)

Philippe
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

nl2605
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
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. :)
Ana
Ana 2014년 3월 21일
Sure it would be a better solution. If I manage to have some extra time in the end I will do it this way which I find much neater...

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


nl2605
nl2605 2014년 3월 20일
  댓글 수: 1
Ana
Ana 2014년 3월 20일
But what is being called in that example is a user function not another Callback function (I had seen this answer before, but I didn't think it solved my problema for that reason).
Of course I need the callback function to work also when pressing directly its associated button.
Should something like this work in my particular case?
Ana

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


Aravinda
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.
Dr.A.S.Aravinda Murthy avadhoth789@gmail.com

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by