Problem in running GUI
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Suraj Gurav
el 15 de Oct. de 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 comentarios
Rik
el 15 de Oct. de 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
el 18 de Oct. de 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 ?
Respuesta aceptada
OCDER
el 16 de Oct. de 2017
Editada: OCDER
el 16 de Oct. de 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 comentarios
Stephen23
el 18 de Oct. de 2017
Editada: Stephen23
el 26 de Oct. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!