GUI: How to update the handles when calling a function Callback inside another Callback?
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
Sara
il 20 Mar 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
Più risposte (4)
Philippe
il 4 Set 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
1 Commento
nl2605
il 20 Mar 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 Commenti
nl2605
il 21 Mar 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. :)
Aravinda
il 23 Ott 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!