GUI: How do save data from one callback to another ?

3 views (last 30 days)
Hi i want to save my data from one callback and use that in other callback.
eg. i have 2 button, one to run the first callback and the other to run the second callback.
function a_Callback(hObject, eventdata, handles)
a = 2+2;
function c_Callback(hObject, eventdata, handles)
c = 2+a;
how do i save or use the " a=2+2 " from the first one to be saved to the next callback, so if i click on "button 2" it will give me eg. 6.

Accepted Answer

Guillaume on 23 Oct 2019
The handles structure that your callback receives is meant exactly for this:
%It's a good idea to create the variable used in the callback when the gui is first created. e.g. in the OpeningFcn callback
function mygui_OpeningFcn(hObject, eventdata, handles)
handles.a = NaN;
function a_Callback(hObject, eventdata, handles)
handles.a = 2+2;
guidata(hObject, handles) %save new state of handles
function c_Callback(hObject, eventdata, handles)
c = 2 + handles.a;

Sign in to comment.

More Answers (0)


Find more on Startup and Shutdown in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by