How to share variables in different callback function?

조회 수: 2 (최근 30일)
QiQin Zhan
QiQin Zhan 2013년 3월 4일
댓글: BHANESH BHADRECHA 2016년 2월 10일
In my code,xdata exists in 'pushbutton1_Callback' function
function pushbutton1_Callback(hObject, eventdata, handles)
handles.xdata = xdata;
guidata(hObject,handles);
Then the xdata exists in handles.However,when I use ‘pushbutton1_Callback’ function in function ‘pushbutton2_Callback’,the xdata somehow disappears.
function pushbutton2_Callback(hObject, eventdata, handles)
pushbutton1_Callback(hObject, eventdata, handles)
I want to know what's wrong with it.And how can I get the xdata in pushbutton2_Callback.Thanks!

채택된 답변

TAB
TAB 2013년 3월 4일
편집: TAB 2013년 3월 4일
function pushbutton1_Callback(hObject, eventdata, handles)
handles = guidata(hObject); % Get gui data
handles.xdata = xdata; % Add your data gui data
guidata(hObject,handles); % Update the gui data with new value
function pushbutton2_Callback(hObject, eventdata, handles)
handles = guidata(hObject); % Get gui data
my_xdata = handles.xdata ; % Get your variable
  댓글 수: 4
Walter Roberson
Walter Roberson 2013년 3월 4일
Please read the documentation on the "guidata" function.
It may be easier if you think in terms of "master copy" (the two-input form of guidata updates the master copy) and "local version" (the one-input form of guidata copies the master copy into a local version that you can then make local revisions to.) And different routines can have different local copies and any of them might update the master copy when the routines have control.
BHANESH BHADRECHA
BHANESH BHADRECHA 2016년 2월 10일
This works perfectly. Thank you very much.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by