Problem in running GUI

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 Commenti

Rik
Rik il 15 Ott 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 il 18 Ott 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 ?

Accedi per commentare.

 Risposta accettata

OCDER
OCDER il 16 Ott 2017
Modificato: OCDER il 16 Ott 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 Commenti

Suraj Gurav
Suraj Gurav il 16 Ott 2017
@Donald Lee
Yes. I got your point about eval function. I did the changes as you suggested which runs the GUI but 3 errors are still there, in line 95 and line 42. could you please suggest anything about it.
So what error message are you receiving? At line 96, I see an assignin. You might not need that since x_max and x_min are used in that callback function and no where else. Do you need x_min and x_max to be saved to the workspace?
x_min=str2double(get(handles.edit2,'String'));
if isempty(x_min)
errordlg('Input Function has to be specified','Input Error');
flag=1;
% else Not needed
% assignin('base','x_min',x_min);
end
x_max=str2double(get(handles.edit3,'String'));
if isempty(x_max)
errordlg('Input Function has to be specified','Input Error');
flag=1;
% else Not needed
% assignin('base','x_max',x_max);
end
if flag==0
xx = x_min:(x_max-x_min)/100:x_max; %This still uses x_min and x_max
....
end
Suraj Gurav
Suraj Gurav il 16 Ott 2017
Yes. I want to store x_min and x_max in the workspace. well the error at line 95 and line 42 are not stopping the execution of my GUI right now. but, I am having another error. Can you please look at .m file attached in this comment. I am getting error at line 127.
OCDER
OCDER il 16 Ott 2017
What is the error message and what are the GUI settings to get the error messages?
One way to debug your GUI is to use the debugger. It will stop the GUI where you put the red stop button. Then evaluate the code line by line by highlighting the code to evaluate and pressing f9. When you encounter the error, look at what all the variables are to see why it's causing an error.
I am converting the string obtained at line 83 into function (line 84). when I run the GUI and and press Evaluate button, the program executes and graph is plotted. In the next step, I chose the point on graph using mouse (line 122). After this point it shows me error in (line 127)
x=x0-(y(x0))/(dydx(x0));
here, in (line 119) I am differentiating the function in y by using diff(y,x) command and I want to get the value of function after differentiating at point x0. In line 127, I am trying to do the same. But, program shows error in evaluating -- x=x0-(y(x0))/(dydx(x0)) -- this value. I am attaching the GUI settings (Image Files) at this point.
<<
>>
Rik
Rik il 17 Ott 2017
Just as with eval, you shouldn't use assignin. For GUIs, you should use guidata. That will enable you to share results of callbacks between functions.
OCDER
OCDER il 17 Ott 2017
Modificato: OCDER il 17 Ott 2017
Yes, Rik is right. assignin should be avoided like eval since they can "poof" variables anywhere, which is hard to track.
About your error, in y(x0), what is x0? x0 must be an integer > 0 since it is used as an matrix index. Example, if x0 = -0.5, what is the -0.5th index of y? This doesn't make sense to matlab, hence you get that error. Same for dydx(x0). I know in math, we write y(x), but in matlab, x is seen as an index and not any number. Read this:
This statement is odd too (read comments):
%dydx=str2func(diff(y)); ERROR: input must be char and no need for (y)
dydx = str2func('diff'); %Or is dydx just a constant vector? dydx = diff(y) ?
tol = 1e-6;
% Should be careful here
set(handles.text5,'String','Please select the initial value with mouse', ForegroundColor','red');
[x,yg]=ginput(1);
for l=1:100
x0=x;
x=x0-(y(x0))/(dydx(x0));
%ERROR: x0 is not an integer > 0, so y(x0) makes no sense
%ERROR: dydx(x0) = [] since you're doing diff(x0).
if abs(x-x0)<tol
break;
end
end
Ya. understood about the assignin.
About the error, the x0 can be the any integer. I am selecting a initial value using mouse, so command
[x,yg]=ginput(1);
gives x and yg as x and y coordinates of selected point. and using x0 = x I am putting value of x into x0. I know the index must be a positive integer but here x0 is not an index, it is a real number (as coordinate of selected point). In the expression
x=x0-(y(x0))/(dydx(x0));
I am finding value of function y and its derivative dydx at any point x0. I am having y and dydx as the function of x. So, y(x0) is expected to give value of function y at any point x0 and dydx(x0) is expected to give value of dydx at any point x0.
is this the correct function you want to use?
To use y(x0) like you want, y must also be a function handle, something like this:
y = str2func('sin') ?
y(0.5) =
0.4794
if I remove dydx(x0) from my program, then it works well but then there will not be any meaning to the question as I want to use the formula as
x=x0-((y(x0))/dydx(x0));
So, I think there is problem only in differentiating the function in y with respect to x and getting its value at any point x0.
I'm looking at your latest code and it says dydx1, not dydx. Is this the issues?
dydx1=diff(y,x); %CHECK THIS. dydx?
tol=1e-6;
% Should be careful here
Unfortunately, I don't have symbolics toolbox, so I can't test it. But you could see if you can evaluate your dydx function for a given x0. Use the debugger and set a breakpoint in the .m file to stop before that is evaluated. Then look at every variables and dydx. See if you can get the simplest derivative to evaluate, for instance this example from the website: https://www.mathworks.com/help/symbolic/diff.html
syms f(x)
f(x) = sin(x^2);
df = diff(f,x)
OCDER
OCDER il 17 Ott 2017
If you can't solve the issue, feel free to post another question that is specific to that error you're getting with a question like:
Error evaluating derivative "y(x0) / dydx(x0)" using symbolic toolbox
And explain the full error, the relevant code, etc. Someone with symbolic toolbox will probably answer that quickly for you.
Suraj Gurav
Suraj Gurav il 18 Ott 2017
Thanks for your help. Finally the GUI worked fine with the eval command
Stephen23
Stephen23 il 18 Ott 2017
Modificato: Stephen23 il 26 Ott 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Functions in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by