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 Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by