matlab says the inline function will be removed in future release how should i execute a string function like '2*x.^2+5'

12 ビュー (過去 30 日間)
function
  1 件のコメント
Stephen23
Stephen23 2017 年 6 月 20 日
When you read the inline documentation the very first line of text says:
inline will be removed in a future release. Use Anonymous Functions instead.
How about using anonymous functions, just like the documentation recommends?

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

採用された回答

Star Strider
Star Strider 2017 年 6 月 20 日
Your function becomes:
fcn = @(x) 2*x.^2+5;
the call it as you would any other function:
y = fcn(x);
To convert a string to a function, use the str2func (link) function:
str = '2*x.^2+5';
fun = str2func(['@(x) ',str]);
y = fun(x);
This creates the anonymous function from the string, and returns a function handle.
  6 件のコメント
Steven Lord
Steven Lord 2017 年 6 月 21 日
Use the symvar function to identify the variable(s) and use the char vector it returns in defining the char vector you pass into str2func.
Alfonso Rodriguez
Alfonso Rodriguez 2021 年 6 月 29 日
Thanks for these comments. They are very useful for computational math.

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 6 月 21 日
syms y
f = matlabFunction(2*y^2+3);

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by