How to construct handle from input string (anonymous function) ?
2 views (last 30 days)
Show older comments
This what I've got so far: Apparently something goes wrong here...
function anon = myIntegrator(X, fun)
fun = input('enter an equation (for example x.^2): ', 's');
X = input('enter variable, separated by comma: ', 's');
%z = input('enter function limit (for example -2,2): ', 's');
anon = str2func(['@(',X,')',fun]);
end
Do I need to use something like regexp or str2double? How and when?
Many thanks in advance Bar
0 Comments
Answers (1)
the cyclist
on 7 Jun 2015
You need to define the function handle first, and then evaluate it for the argument. I don't think you can do that all in one step.
Also, because you are asking for the inputs from the console, I took them out of the function call of myIntegrator. Here is the code I used. Just call it with "myIntegrator()".
function output = myIntegrator()
fun = input('enter an equation (for example x.^2): ', 's');
X = input('enter variable, separated by comma: ');
anon = str2func(['@(x)',fun]);
output = anon(X);
end
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!