MATLAB: I am unable to store data in an Object to use in a different callback function using GUI

callbackguimatlab gui

% --- Executes on button press in strline.
function strline_Callback(hObject, eventdata, handles)
% hObject handle to strline (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB



% handles structure with handles and user data (see GUIDATA)


cla(handles.axes1,'reset');
x=linspace(-10,10,500);
axes(handles.axes1)
hold on
axis([-10 10 -15 15])
grid on
line(xlim,[0 0],'color','k','linewidth',3)
line([0 0],ylim,'color','k','linewidth',3)
y=x;
plot(x,y)
handles.Y=y
hold off
gatherandupdate(handles)
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
gatherandupdate(handles,hObject)
function gatherandupdate(handles)
gathereddata=gatherdata(handles)
updateaxes(handles.axes1,gathereddata)
function gathereddata = gatherdata(handles)
gathereddata.dong=get(handles.popupmenu3,'string')
if strcmp(gathereddata.dong,'red')
gathereddata.g=[1 0 0]
elseif strcmp(gathereddata.dong,'green')
gathereddata.g=[1 1 0]
else strcmp(gathereddata.dong,'blue')
gathereddata.g=[1 0 1]
end
function updateaxes(ax1,gathereddata)
axes(ax1);
axis([-10 10 -15 15])
grid on
line(xlim,[0 0],'color','k','linewidth',3)
line([0 0],ylim,'color','k','linewidth',3)
x=linspace(-10,10,500);
t=handles.Y
plot(x,t,'color',gathereddata.g);
hold off
I would like to store use the variable from str_linecallback() to the function updateaxes. I did refer the solutions of matlab users who had similar problems but I couldnt find a way to make mine work. Thanks.

Best Answer

The general idea is, that the handles struct is stored inside the figure's ApplicationData. If you have created a GUI with GUIDE, the handles struct contains the current value, when a callback is called:
function YourCallback(hObject, EvenData, handles)
% Now handles is the current value

end
If you modify handles, store the new value in the figure again:
function YourCallback(hObject, EvenData, handles)
% Now handles is the current value
handles.time = clock;
guidata(hObject, handles);
end
Then the updated struct is available in other callback also.
Related Question