GUI function from Edit

Hello matlab people community
I am so new to use matlab. I try to take some function respect to x, from edit1 which is name of the component, then I push a button , function of plot will see on the axes? I need a code how to call the function from the edit?
I write the codes here
function pushbutton1_Callback(hObject, eventdata, handles)
axes(handles.axes1);
x=0:0.1:10;
handles.a = get(handles.edit1,'String');
plot(x,a);
Thanks!!!

3 comentarios

engineerOfPhysics
engineerOfPhysics el 13 de Mzo. de 2013
Heey ! Guys I need jıust some code examples. I looked whole site but I did not find any helpful things about that situation.
Jan
Jan el 13 de Mzo. de 2013
The question is not clear. Therefore bumping is a waste of time only.
What does this mean: "Try to take some function repsect to x"? What is "x" and "some function"? What does "function of plot will see on the axes"? What is the contents of the string obtained from the edit field?
engineerOfPhysics
engineerOfPhysics el 13 de Mzo. de 2013
It means for example I write sin(x) then I push the button on the axes the graph of sinx will be seen. In the same GUI ?

Iniciar sesión para comentar.

 Respuesta aceptada

engineerOfPhysics
engineerOfPhysics el 13 de Mzo. de 2013

0 votos

How to convert string to function in GUİ ? come on guys I need your help !!

6 comentarios

Alessandro
Alessandro el 14 de Mzo. de 2013
Now you have the question here is the answer:
string = '@(x)(x.^2)' % From edit field !!
func = eval('@(x)(x.^2)')
y = func(x)
engineerOfPhysics
engineerOfPhysics el 15 de Mzo. de 2013
I tried but I think I had a mistake. I write here my codes and you can tell me where I wrong or I miss sth. Thanks...
if true
function edit1_Callback(hObject, eventdata, handles)
string = '@(x)(x.^2)' % From edit field !!
func = eval('@(x)(x.^2)');
y = func(x);
function pushbutton1_Callback(hObject, eventdata, handles)
x = 0:0.1:10;
axes(handles.axes1);
plot(x,y);
end
Alessandro
Alessandro el 15 de Mzo. de 2013
Editada: Alessandro el 18 de Mzo. de 2013
Hmm ok my comment was wrong here is the complete solution for what you are trying to do:
function pushbutton1_Callback(hObject, eventdata, handles)
axes(handles.axes1);
x=0:0.1:10;
tmp = get(handles.edit1,'String');%Get the edit field string
%Transform function in anonymous function:
anomfunc = ['@(x)' tmp];%String hack
func = eval(anomfunc);%now you have the anonymous function
plot(x,func(x));done !
To test it write in the edit field --> sin(x)
engineerOfPhysics
engineerOfPhysics el 15 de Mzo. de 2013
Thanks for your all wasting time for me my friend but still there is a problem. I could not get any graph on the axes.
Undefined function 'eval' for input arguments of type 'cell'.
Error in plotter4>edit1_Callback (line 82) func = eval(anomfunc);%now you have the anonymous function
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in plotter4 (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)plotter4('edit1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
there are the words explain the error...
Pls help me again :))
engineerOfPhysics
engineerOfPhysics el 19 de Mzo. de 2013
Yeap sorry for delaying. Your code is working so nice. Thanks for your helping..
Image Analyst
Image Analyst el 19 de Mzo. de 2013
Just FYI: generally you don't post an "Answer" to your own question when it's really a comment, and you don't accept _you_r Answer unless you were really the person who came up with the solution (instead of Allessandro).

Iniciar sesión para comentar.

Más respuestas (2)

Alessandro
Alessandro el 13 de Mzo. de 2013
Editada: Alessandro el 13 de Mzo. de 2013

0 votos

you got there a string maybe you should first convert it to an array ! Try this simple solution:
handles.a = get(handles.edit1,'String');
tmpa = str2num(handles.a)
plot(x,tmpa);

2 comentarios

engineerOfPhysics
engineerOfPhysics el 13 de Mzo. de 2013
I need a function sth like str2func. Is there any function like that. I put a function on the edit compenent the I want to use it. the function for example sin(x) or x^2 or 3*x+5. I do not need str2num. Is there any way to do that.
engineerOfPhysics
engineerOfPhysics el 13 de Mzo. de 2013
And I try your solution does not work

Iniciar sesión para comentar.

engineerOfPhysics
engineerOfPhysics el 13 de Mzo. de 2013
Editada: engineerOfPhysics el 13 de Mzo. de 2013

0 votos

I am working on a GUI. I want to draw a function. I want to take the function which can be sin(x) or x^4 or 3*x+5 from the user, then when I push the button the graph of the function will be seen on the asex1. User writes function on the edit1 component.
Is there any way to do that? I need a some code which make convert the string to the function so GUI can plot. ?
Please help. I could not solve ..
Thanks...

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by