How to construct handle from input string (anonymous function) ?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
the cyclist
2015-6-7
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!