plot graph in GUI

조회 수: 3 (최근 30일)
sagi ash
sagi ash 2016년 11월 1일
답변: Geoff Hayes 2016년 11월 6일
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.

답변 (1개)

Geoff Hayes
Geoff Hayes 2016년 11월 6일
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.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by