MATLAB: Adding elements into a vector using MATLAB GUI

callbackfunctionhandlesmatlab guivector

I am new to matlab and I'm trying to make a progrem which gets parameters from the user(using gui)for x value and y value,puts them into 2 different vectors and when the user is done it uses plot to make a graph out of the two vectors.
here is what I tries to do:
function pushbutton1_Callback(hObject, eventdata, handles)
x=[];
y=[];
a=str2double(get(handles.edit1,'string'));
b=str2double(get(handles.edit2,'string'));
handles.x=[x a];
handles.y=[y b];
guidata(hObject, handles);
function Doit_Callback(hObject, eventdata, handles)
axes(handles.axes1)
handles.x;
handles.y;
handles.m=handles.x;
handles.n=handles.y;
plot(handles.m,handles.n);
guidata(hObject, handles);
function Doit_Callback(hObject, eventdata, handles)
axes(handles.axes1)
handles.x;
handles.y;
handles.m=handles.x;
handles.n=handles.y;
plot(handles.m,handles.n);
guidata(hObject, handles);
but the plot function won't work.I'm tring to get a and b from the edit text in the gui and put them into vector x and vector y,and when I'm done adding all the element I want I'm trying to use the full vectors in the Doit function. any help would be appreciated

Best Answer

You have
x=[];
y=[];
so you set x and y to empty arrays
a=str2double(get(handles.edit1,'string'));
b=str2double(get(handles.edit2,'string'));
so you get scalar a and b values from what the user entered
handles.x=[x a];
handles.y=[y b];
x and y are both empty because you set them that way, so this is equivalent to
handles.x = a;
handles.y = b;
which sets scalars. But it sounds like later you expect handles.x and handles.y to be a record of everything the user ever entered.
I suspect you want
if ~isfield(handles, 'x')
handles.x = [];
handles.y = [];
end
a = str2double(get(handles.edit1,'string'));
b = str2double(get(handles.edit2,'string'));
handles.x(end+1) = a;
handles.y(end+1) = b;
guidata(hObject, handles);
Note: in your callbacks, the lines
handles.x;
handles.y;
are not doing anything useful and can be removed.
Related Question