How to share variables in different callback function?

2 Ansichten (letzte 30 Tage)
QiQin Zhan
QiQin Zhan am 4 Mär. 2013
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!

Akzeptierte Antwort

TAB
TAB am 4 Mär. 2013
Bearbeitet: TAB am 4 Mär. 2013
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 Kommentare
Walter Roberson
Walter Roberson am 4 Mär. 2013
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 am 10 Feb. 2016
This works perfectly. Thank you very much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Objects finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by