Problem in running GUI

조회 수: 3 (최근 30일)
Suraj Gurav
Suraj Gurav 2017년 10월 15일
편집: Stephen23 2017년 10월 26일
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
Rik
Rik 2017년 10월 15일
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 2017년 10월 18일
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 ?

댓글을 달려면 로그인하십시오.

채택된 답변

OCDER
OCDER 2017년 10월 16일
편집: OCDER 2017년 10월 16일
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
Suraj Gurav
Suraj Gurav 2017년 10월 18일
Thanks for your help. Finally the GUI worked fine with the eval command
Stephen23
Stephen23 2017년 10월 18일
편집: Stephen23 2017년 10월 26일
"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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Functions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by