plot graph in GUI
3 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Geoff Hayes
on 6 Nov 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
0 Comments
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!