Trouble with function called from a GUI callback fonction
1 次查看(过去 30 天)
显示 更早的评论
I use GUIDE to make an interface.
I obtain some callbacks in wich I want to use another function as follow:
function CallbackFunction(hObject, eventdata, handles)
...
myFunction(hObject, eventdata, handles);
...
guidata(hObject, handles);
end % function
fucntion myFunction(hObject, eventdata, handles)
...
handles.Var=
guidata(hObject, handles);
end % fonction
But this seems to cause me many problems. I seem to miss something.
I just want to write a function that is called from a callback function and that can change handles variables?
0 个评论
采纳的回答
Geoff Hayes
2016-10-20
Claude - look closely at the order in which handles is updated. The
function CallbackFunction(hObject, eventdata, handles)
is called passing in the latest (at the time) version of handles. This parameter (along with the other two) are passed in to your function
function myFunction(hObject, eventdata, handles)
where the code updates the handles object and then saves it using
guidata(hObject, handles);
But then we return back to your CallbackFunction and maybe do something, and then calls
guidata(hObject, handles);
But which version of handles is this? This is the copy that was passed into this function when CallbackFunction was called. It is not the updated version from your call to myFunction since myFunction does not return the updated handles structure, and your code probably doesn't refresh handles (after this call to myFunction) with
handles = guidata(hObject);
So you can do at least two different things - return the updated handles structure from myFunction by changing its signature to
function [handles] = myFunction(hObject, eventdata, handles);
and so your code would become something like
function CallbackFunction(hObject, eventdata, handles)
...
handles = myFunction(hObject, eventdata, handles);
...
guidata(hObject, handles); % in case you've updated handles again
end % function
Or you can refresh your local copy as
function CallbackFunction(hObject, eventdata, handles)
...
myFunction(hObject, eventdata, handles);
handles = guidata(hObject);
...
guidata(hObject, handles); % in case you've updated handles again
end % function
Hopefully, one of the above solutions will work for you!
0 个评论
更多回答(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!