How to share variables in different callback function?

2 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

TAB
TAB il 4 Mar 2013
Modificato: TAB il 4 Mar 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 Commenti
Walter Roberson
Walter Roberson il 4 Mar 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 il 10 Feb 2016
This works perfectly. Thank you very much.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Objects in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by