GUI: How to update the handles when calling a function Callback inside another Callback?
15 次查看(过去 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!