How to plot a function with GUI input data?

Hi, I'm new to MATLAB and GUI building and I have problems to plot a function which is given by text edit input.
Here is my code so far.
function pb_plot_Callback(hObject, eventdata, handles)
f_x= get(handles.ed1,'String');
display (f_x); %f_x is a String i.e. 'x^2 -2'
x1= get(handles.ed2,'String');
x= str2num(x1);
display (x); % x= -2:0.1:2
f_x= [1^2 0 -2]; %How do I convert f_x = 'x^2 -2' into this?
%x= -2:0.1:2
y= polyval(f_x,x);
plot(x,y);
%symbolic solution doesnt work at all
%syms a b
%a= f_x;
%b= x;
%c= subs(a);
%plot(b,c);
So as you can see for polyval I need to convert f_x to a matrix wothout variables.
The symbolic solution results in this error:
??? Subscript indices must either be real positive integers or
logicals.
Error in ==> sym.subs>getNames at 248
s(end) = [];
Error in ==> sym.subs>tryFunctionHandle at 190
xvarnames = getNames(X);
Error in ==> sym.subs>mupadsubs at 137
[G,worked] = tryFunctionHandle(F,X,Y);
Error in ==> sym.subs at 125
G = mupadsubs(F,X,Y);
Error in ==> subs at 60
r = subs(sym(f),varargin{:});
Error in ==> mathsuite>pb_plot_Callback at 153
c= subs(a);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> mathsuite at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)mathsuite('pb_plot_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
so I think symbolic doesn't work at all in GUI callbacks.
I hope you can help me. Thanks in advance.

Answers (0)

Asked:

on 3 Feb 2015

Commented:

on 4 Nov 2019

Community Treasure Hunt

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

Start Hunting!