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

39 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Sara
Sara am 20 Mär. 2014
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 Kommentar
Ana
Ana am 20 Mär. 2014
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Philippe
Philippe am 4 Sep. 2014
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 am 20 Mär. 2014
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 Kommentare
nl2605
nl2605 am 21 Mär. 2014
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 am 21 Mär. 2014
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...

Melden Sie sich an, um zu kommentieren.


nl2605
nl2605 am 20 Mär. 2014
  1 Kommentar
Ana
Ana am 20 Mär. 2014
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

Melden Sie sich an, um zu kommentieren.


Aravinda
Aravinda am 23 Okt. 2014
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

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by