How to plot a function, typed in a textfield (GUI), as a function handle?

Why does the following don't work or what needs to be added?
% --- Executes on button press in pbCalculate.
function pbCalculate_Callback(hObject, eventdata, handles)
x=handles.VarXStart:handles.VarXRes:handles.VarXEnd;
fh=@(x) handles.Function;
plot(x,fh(x))
"handles.Function" contains for example "x.^2" or "sin(x)" (it's a string). Typed into a textfield in the GUI by a user.
I've googled now a whole day long and couldn't find anything that worked... thanks for your help!

 採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 17 日

0 投票

I am not sure if anonymous function can be defined that way. Try this:
str='x.^2';
f=inline(str);
x=0:0.1:4;
plot(x,f(x))

1 件のコメント

Lukas
Lukas 2011 年 9 月 17 日
Thank you very much! "inline()" works well.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by