Problem in running GUI

1 Ansicht (letzte 30 Tage)
Suraj Gurav
Suraj Gurav am 15 Okt. 2017
Bearbeitet: Stephen23 am 26 Okt. 2017
I am creating a GUI for Newton Raphson method. The code I wrote works well but when it comes to GUI it shows strange error. following is the error. Could anyone tell me how to get solved this error.
newtonraphson
y =
1×1 cell array
{'sin(x)'}
Error using eval Must be a string scalar or character vector.
Error in newtonraphson>pushbutton1_Callback (line 109) yy=eval(y)
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in newtonraphson (line 42) gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)newtonraphson('pushbutton1_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating UIControl Callback.
I have attached the .fig file also, so that you can check it.
  2 Kommentare
Rik
Rik am 15 Okt. 2017
You did three things that is very effective in discouraging people to answer your question: very poor formatting, a wall of text and saying your problem is urgent.
Just looking at the last few lines I would say the error message tells you what you need to change: y is a cell containing a string, while it used in a function that expects a char vector.
Stephen23
Stephen23 am 18 Okt. 2017
You are creating a GUI for the Newton-Raphson method, and you need slow, buggy, totally inefficient eval ? Why not simply stick to faster, simpler, and much more efficient numeric calculations ?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

OCDER
OCDER am 16 Okt. 2017
Bearbeitet: OCDER am 16 Okt. 2017
Don't use eval because it's way too powerful of a function that leads to issues and is slow. It's like using a chain saw to cut a birthday cake (doing a simple task) - it works, but you'll end up with a big mess (unable to debug), it was way too dangerous to use (can overwrite / delete any var or files), and you just might destroy the table holding the cake (you'll have to rebuild your program and unlearn the eval habit) ...
To fix, rewrite your code at pushbutton1_Callback (line 109):
instead of:
yy = eval('sin(x)') or yy = eval(y{1})
just do:
yy = sin(x)
or if it's not always a sine function, then:
fh = str2func(['@(x)' y{1}])); %creates a function handle like @(x) sin(x)
yy = fh(x); %evaluates sin(x)
  14 Kommentare
Suraj Gurav
Suraj Gurav am 18 Okt. 2017
Thanks for your help. Finally the GUI worked fine with the eval command
Stephen23
Stephen23 am 18 Okt. 2017
Bearbeitet: Stephen23 am 26 Okt. 2017
"Finally the GUI worked fine with the eval command"
I have written hundreds of MATLAB GUIs from scratch, and never needed to use slow, buggy, awful eval or assignin. Note that <using eval to evaluate symbolic equations is the wrong tool for that task, as Steven Lord explains here:
Also worth reading:
You might like to consider the advice that people are giving you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Functions finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by