Why Is Not the Handles Variable Updated?

10 ビュー (過去 30 日間)
Rightia Rollmann
Rightia Rollmann 2017 年 3 月 17 日
回答済み: Jan 2017 年 3 月 17 日
I have made a simple GUI in GUIDE. I update field A by function myfun1 to the value 20, but I still get 1 in the Command Window! Why hasn’t handles variable been updated? What is the solution? I’d appreciate it if you explain it elaborately
function pushbutton1_Callback(hObject, eventdata, handles)
handles.A = 1;
guidata(hObject, handles);
myfun1(hObject, eventdata, handles)
disp(handles.A)
function myfun1(hObject, eventdata, handles)
handles.A = 20;
guidata(hObject, handles);
  2 件のコメント
Jan
Jan 2017 年 3 月 17 日
+1. I've voted for this thread. The question is clear and it is a common problem. The different answers and comments focus on different viewpoints and complement each other. I made a typo in my marginal note, and Walter has clarified it. This is the kind of cooperative teamwork I like in this forum. If a reader has a problem with the updates of the "handles" struct, reading this thread will help.
Thanks, Adam, Chocolate, Rightia and Walter.
Sorry, this comment might look off-topic. I think that positive feedback is more satisfying than complains in case or problems.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2017 年 3 月 17 日
Each routine has its own local copy of the handles structure that (normally) is copied in from the master copy of the handles structure when the routine starts executing. When guidata is called with two inputs the master copy of the handles structure is updated, but no local copy is updated. The handles structure is not a global variable: it is more like "go take a photocopy of the current master and bring back the copy" together with "go file this as the master" -- the photocopies that already exist do not get updated.
If you are in a callback with a copy of the handles structure and you have reason to suspect that the master might have been updated after you got your copy, then if you want to know what the master says now you need to guidata with one input to fetch a new copy of the current master.

その他の回答 (2 件)

ES
ES 2017 年 3 月 17 日
Your code should work.
Alternatively you can make myfun1 to return handles instead of using guidata to update handles.
function pushbutton1_Callback(hObject, eventdata, handles)
handles.A = 1;
guidata(hObject, handles);
handles = myfun1(hObject, eventdata, handles)
disp(handles.A)
function handles = myfun1(hObject, eventdata, handles)
handles.A = 20;
  4 件のコメント
Jan
Jan 2017 年 3 月 17 日
@Walter: Thanks for the clarification and finding the bug in my code. See [EDITED]: I had copied Rightia's code accidently, but Chocolate's code contained the required "handles=". Now I understand what "does not work" was related to.

サインインしてコメントする。


Jan
Jan 2017 年 3 月 17 日
Sharing data between callbacks is a frequently occurring problem. Therefore searching in the forum is most likely useful:

Community Treasure Hunt

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

Start Hunting!

Translated by