How to modify the handles structure (GUI) from an external function

조회 수: 3 (최근 30일)
MRR
MRR 2013년 3월 8일
답변: zakaria 2014년 6월 5일
Hi,
I am developing a GUI. I can modify the handles structure (for example handles.variable1 = 2) within callbacks. However I am not able to do the same within an external function. E.g., this does not work:
function [matrix, indexVector] = myFunction(handles)
handles.variable1 = 1;
It is important to note that myFunction does not belong to the GUI callbacks, it is simply a function I have created in another m file. When I call it I can read the handles struture (actually im passing them as a parameter) but when I try to modify them the modification is not recorded.
Best,
-MRR

채택된 답변

Sean de Wolski
Sean de Wolski 2013년 3월 8일
편집: Sean de Wolski 2013년 3월 8일
function [matrix, indexVector] = myFunction(handles)
handles.variable1 = 1;
guidata(handles.figure1,handles)
Where figure1 is the 'Tag' of the figure. You want to make sure that you assign the handles structure to the right place (i.e. the figure). hObject is dynamic, handles will always have the field for the figure.
  댓글 수: 10
Jan
Jan 2013년 3월 20일
편집: Jan 2013년 3월 20일
@MRR: Yes, of course, this is the expected behavior. Inside myFunction you update the handles struct and store it in the figure afterwards. Then after returned to Simulate_Callback, you use the local copy of handles with its value before calling myFunction() and overwrite the changes from inside this function. A suggestion for improvements:
function errorParameters = Simulate_Callback(hObject, eventdata, handles)
%some code here ...
handles = guidata(hObject); % Care for the newest version explicitly!
[output1, output2] = myFunction(hObject, handles);
handles = guidata(hObject); % Get the version updated in myFunction!
handles.variable1 = handles.variable1 + 1;
guidata(hObject, handles);
And inside myFunction(hObject, handles) I do:
handles.variable1 = 200; %or any other value
guidata(hObject, handles);
So Azzi's and Sean's answers hit the point already, but in your code there was a problem at another location, namely in the caller, which forgot to obtain the updated struct.
MRR
MRR 2013년 3월 20일
@Jan Simon: Thank you, now it works. The key instruction that makes the handles structure to be updated is (and as you can see it wasnt used in any other previous solution in this post):
handles = guidata(hObject); % Get the version updated in myFunction!
But bear in mind that the other solutions (mine included) did not work, i.e., using guidata(hObject, handles) does not update the handles structure modified by myFunction, however it works for any callback function defined in the gui (i.e, those functions that are automatically associated with the pushbuttons, etc..), so Azzi's and Sean's answers, although hit the point, do not work. Of course, their and your feedback is very appreciated.
All in all, I would say the way GUIDE manages the handles structure is confusing.
Hope this discussion will help another "gui developers"
Best and thank you!
-MRR

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

추가 답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 8일
Use
function [matrix, indexVector] = myFunction(hObject,handles)
handles.variable1 = 1;
guidata(hObject,handles)
  댓글 수: 4
MRR
MRR 2013년 3월 20일
Thanks Jan Simon. Detailed in Sean's answer is my explanation.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 20일
MRR, if so then accept his answer

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


QiQin Zhan
QiQin Zhan 2013년 3월 8일

zakaria
zakaria 2014년 6월 5일
handles = guihandles(gcbo); % if not presents in the other external function

카테고리

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