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 Center および File ExchangeGraphics Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by