Problem in running GUI

10 ビュー (過去 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 Center および File ExchangeFunction Creation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by