MATLAB: Hi guys, i want to plot in GUI, i’ve tried this code.

handles.mydataplot

function calcbutton_Callback(hObject, eventdata, handles)
dis = sqrt((handles.mydata.b)^2 -4*(handles.mydata.a)*(handles.mydata.c));
ans1 = (-(handles.mydata.b) + dis) /(2*(handles.mydata.a));
ans2 = (-(handles.mydata.b) - dis) /(2*(handles.mydata.a))
set(handles.ans1, 'String', ans2);
set(handles.ans2, 'String', ans1);
function plotbutton_Callback(hObject, eventdata, handles)
x=0:10;
plot(x,handles.ans1, handles.axes1);
hold on;
plot(x,handles.ans2, handles.axes2);
But it displays:
Error using plot
Data must be a single matrix Y or a list of pairs X,Y.

Best Answer

Get rid of the calls to set and just do this:
handles.ans1 = (-(handles.mydata.b) + dis) /(2*(handles.mydata.a));
handles.ans2 = (-(handles.mydata.b) - dis) /(2*(handles.mydata.a))
In the second callback do this instead:
x=0:(length(handles.ans1) - 1);
plot(x,handles.ans1, handles.axes1);
grid on;
hold on;
x=0:(length(handles.ans2) - 1);
plot(x,handles.ans2, handles.axes2);
If it still doesn't work, then find a call to guidata() somewhere in your code and copy it to the last line of the callback that assigns handles.ans1 and handles.ans2.
Related Question