Kevin Zheng
Kevin Zheng

Reputation: 21

Matlab GUI, share data among callback functions

It took me a long time to do this. I can search a lot of tutorial teaching this, but I cannot do it anyway, so I need some help and have to ask question here. In GUI, there are two listboxes and two push buttons. I click pushbutton2 and we get data. Now in function pushbutton1_callback, I need to use data. I don't want to use 'Global' because it's not a good method. Thanks a lot.

function pushbutton1_Callback(hObject, eventdata, handles)

% I need to get  data from function pushbutton2

function pushbutton2_Callback(hObject, eventdata, handles)

data = get(handles.listbox2,'String')

Upvotes: 0

Views: 242

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

Use the handles structure of the GUI to store all the data you want. Look here as well.

function pushbutton2_Callback(hObject, eventdata, handles)

handles.data = get(handles.listbox2,'String') % Store the data directly in  the structure.

guidata(handles,hObject); %// update the structure. Important!

function pushbutton1_Callback(hObject, eventdata, handles)

%// Here use the data as you wish:

DatatoUse = handles.data;

and you're good to go.

Upvotes: 1

Related Questions