plot graph in GUI

4 visualizaciones (últimos 30 días)
sagi ash
sagi ash el 1 de Nov. de 2016
Respondida: Geoff Hayes el 6 de Nov. de 2016
hi. I want to use the GUI for show graph of a functions. The function is given by the user in the "edit text" like : 2*x+x^3 I also put a "Push_button" and "Axes".
i want that for pushing the button, i will see the graph of the function in the "Axes". but i am having trouble in the input function that i get from the user. I need to convert that function to something, i dont know.
thank you.

Respuestas (1)

Geoff Hayes
Geoff Hayes el 6 de Nov. de 2016
sagi - consider using str2func which will convert the character string representation (of your function) into a function handle. Note that you may need to prepend the string with @(x) or whatever variable is being passed into this function. For an example, if an user enters the following in an edit control of your GUI, then you would do the following in the push button callback
function pushbutton1_Callback(hObject,eventdata,handles)
funcString = get(handles.edit1,'String');
funcHandle = str2func(['@(x)' funcString]);
x = linspace(0,4,100);
y = funcHandle(x);
plot(handles.axes1,x,y);
The above assumes that the x is the only input parameter and that the input string supports element-wise operations where necessary. For example, since this example passes an array into the funcHandle, your function string would need to be defined as
2*x+x.^3
Note the element-wise power operation.

Categorías

Más información sobre String Parsing en Help Center 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