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

12 Ansichten (letzte 30 Tage)
function
  1 Kommentar
Stephen23
Stephen23 am 20 Jun. 2017
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 20 Jun. 2017
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 Kommentare
Steven Lord
Steven Lord am 21 Jun. 2017
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 21 Jun. 2017
syms y
f = matlabFunction(2*y^2+3);

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by