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

1 visualizzazione (ultimi 30 giorni)
% --- 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.
  1 Commento
Jan
Jan il 23 Mag 2019
"I would like to store use the variable from str_linecallback() to the function updateaxes" - which variable?
If you update the handles struct by:
handles.Y=y
remember to store the new value in the figure:
guidata(hObject, handles)

Accedi per commentare.

Risposta accettata

Jan
Jan il 23 Mag 2019
Modificato: Jan il 23 Mag 2019
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.
  2 Commenti
Vaidhyanathan C
Vaidhyanathan C il 23 Mag 2019
Thanks, I did add this part,
guidata(hObject, handles);
into my program but it seems that the compiler cannot read the data from the handle.
I get this error,
Undefined variable "handles" or class "handles.Y".
Error in untitled>updateaxes (line 208)
t=handles.Y
Is there anything wrong with my arguments passed into the updateaxes() function?

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by