Warm tip: This article is reproduced from stackoverflow.com, please click
function matlab string

Matlab function defintion method that takes string as the definition (MFILE)

发布于 2020-04-10 10:15:36

there is this method I used the other day and I have forgotten the details, which in we used a syntax like this:

f=//command//(x,'sin(x)');

something like this. im not sure if the syntax is fully correct, or what the right command is. but after this we could simply ask for the f(x) value like this:

x= 0;
y= f(x);

and then the results were y=0;

Questioner
hmg373
Viewed
67
Daniel 2020-02-01 21:15

What you are asking for is usually not recommendable. Please check if a simple anonymous function also fits your requirements:

f=@(x)(sin(x))

In case you really need to evaluate from a string:

f=str2func('@(x)sin(x)')

I would advice against the second option unless absolutely required, it can lead to hard to debug errors.