Problem plotting function in GUI
显示 更早的评论
Hello everyone, this is the first time I post here so if I sound like a newbie is because I am. I have a problem when trying to plot a function in GUI so I decided to do some tests in order to figure it out. I'm creating a non-linear equation solver using Newton-Raphson Method and this is how the UI looks (is in Spanish but is quite intuitive):

Funcion = The function the user wants to solve. Stored in the variable 'funcion' and then I use the inline() function in order to get a function that I can use in order to compute values (stored in the variable f). When he clicks on "Click aqui para mostrar la derivada", the program derivates the function the user wants to solve in order to use it in the formula. Valor inicial = Initial value. "Elija el metodo" = The user can choose one of two methods, fixed number of iterations or tolerance (hope this is clear). "Calcular" = The method executes and finds the value wanted.
I haven't implemented the table yet so I see the answer in the command window. Until this point, everything works smoothly, the root is stored in the variable 'x1'. The problem begins when I click the push button called "Graficar respuesta". What it does is to plot the function and a circle around the point 'x1' which represents one root of the function, at least that's what is supposed to do. The code is:
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
global x1;
global funcion;
global f;
if x1==0
x1=x1+1;
elseif x1<0
x1=-x1;
end
% axes(handles.axes2);
%Imagen
figure (1);
x=-5*x1:0.001:5*x1;
plot(x,funcion,'Color','red'), title('Grafica de la funcion'), xlabel('Eje X'), ylabel('Eje Y'), grid;
hold on;
scatter(x1,f(x1));
But I get the next error:
Error using plot
Error in color/linetype argument.
Error in newton>pushbutton3_Callback (line
196)
plot(x,funcion,'Color','red'), title('Grafica
de la funcion'), xlabel('Eje X'), ylabel('Eje
Y'), grid;
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in newton (line 18)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)newton('pushbutton3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
What am I doing wrong? I would really appreciate any help, please. Thanks in advance.
6 个评论
Geoff Hayes
2018-6-28
Diego - is funcion a handle to a function or an array of values? If the former, where do you evaluate it? Please show more of your code.
Also, consider alternatives to global variables. Use the handles structure to manage some user-defined data.
Rik
2018-6-28
For an actual solution to this problem: use 'r' instead of 'red'.
I agree with Geoff on both counts: use guidata instead of globals, and where do you process funcion to a vector that doesn't depend on x but has the same number of elements? Because that's likely to be the next error from that line of code.
Diego Guisasola Plejo
2018-6-29
编辑:Diego Guisasola Plejo
2018-6-29
"I'm new to Matlab"
That is a good reason to avoid global variables: it is easier to learn good habits from the start than unlearn bad habits later.
"As for the global variables, I don't feel too comfortable using handles."
In GUI's handles is just a structure. It has fields, just like every other structure. Get data out, put data in, store it using guidata. Not complex, and much better than using global variables!
Diego Guisasola Plejo
2018-6-30
Stephen23
2018-7-1
Avoid using global variables, evalin, and assignin. For callback functions use guidata (for GUIDE) or nested functions (if you are writing your own code). For non-GUI code pass variables as input/output arguments.
You will find plenty of simple examples on this forum, e.g.:
Using guidata is very simple:
function foobar(hObject, eventdata, handles)
...
... handles.X ... % access field X
handles.Y = ... % define field Y
...
guidata(hObject,handles) % save any new data
回答(1 个)
Rik
2018-6-29
Using the handles struct is very easy. It may seem hard to learn good practices, but it is much harder to unlearn bad practices. The code below works on my system (R2018a). You should really pay attention to the m-lint warnings. Replacing an old function like inline with anonymous functions is a good place to start. In your call to that figure 1, I would add a clf(1), especially since you have hold turned on.
function funcion_Callback(hObject, ~, handles)
syms x;
funcion=get(handles.funcion,'string');
f=str2func(['@(x)' funcion]);
df=symfun(diff(f,x),x)
set(handles.derivada,'string',char(df));
handles.funcion=funcion;
handles.df=df;
handles.f=f;
guidata(hObject,handles)
function pushbutton3_Callback(hObject, ~, handles)
x1=handles.x1;%global x1;
%funcion=handles.funcion;%global funcion;
f=handles.f;%global f;
if x1==0
x1=x1+1;
elseif x1<0
x1=-x1;
end
% axes(handles.axes2);
%Imagen
figure (1);
x=-5*x1:0.001:5*x1;
plot(x,f(x),'Color','red'), title('Grafica de la funcion'), xlabel('Eje X'), ylabel('Eje Y'), grid;
hold on;
scatter(x1,f(x1));
6 个评论
Diego Guisasola Plejo
2018-6-30
Rik
2018-6-30
About the tilde syntax: it is used to ignore an unused input, and as you don't use the eventdata in your callback, you can ignore it. This will remove the warning in the mlint (the orange underlining and the orange indicator in the scroll bar). Clearing all warnings will help you keep a clean code.
About the errors you are getting: you need to make sure all variables are in the handles struct when you want to use them. You can think about it the same way as with globals: you need to set them, otherwise they'll be empty.
I don't have the time to carefully look at your code right now, probably Tuesday I'll have more time, so you should try to fix it yourself.
Diego Guisasola Plejo
2018-7-1
编辑:Diego Guisasola Plejo
2018-7-1
Rik
2018-7-3
It looks like at some point x1 is a sym instead of a double. You should use the debugger to step through your code so you can see where the data type is changed.
Diego Guisasola Plejo
2018-7-4
编辑:Diego Guisasola Plejo
2018-7-4
Rik
2018-7-4
You could replace
plot(x,f(x),'Color','red')
by
plot(x,arrayfun(f,x),'Color','red')
%untested, you might need to add 'UniformOutput',0 to arrayfun
That way the function is applied to each element separately, which makes it slower, but will prevent odd errors in cases of powers, multiplications, and divisions.
类别
在 帮助中心 和 File Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

