adding elements into a vector using MATLAB GUI

13 views (last 30 days)
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
  2 Comments
Walter Roberson
Walter Roberson on 28 Feb 2018
Are a and b intended to be vectors? Because str2double() only works for strings representing scalars. If this is the problem then str2num() works on strings representing scalars -- but is more fragile and less secure, since it is happy to accept input such as
delete('*.*')
and act on it...
elizabeth march
elizabeth march on 28 Feb 2018
No,a and b intended to sort the variables I get from the edit text boxes in the gui,and I want to insert a into x and b into y. x and y intended to be the vectors

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Feb 2018
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!