MATLAB: Is Not the Handles Variable Updated

guidehandles variable

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);

Best Answer

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.