How to share variables in different callback function?

3 visualizaciones (últimos 30 días)
QiQin Zhan
QiQin Zhan el 4 de Mzo. de 2013
Comentada: BHANESH BHADRECHA el 10 de Feb. de 2016
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!

Respuesta aceptada

TAB
TAB el 4 de Mzo. de 2013
Editada: TAB el 4 de Mzo. de 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 comentarios
Walter Roberson
Walter Roberson el 4 de Mzo. de 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 el 10 de Feb. de 2016
This works perfectly. Thank you very much.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by