How can I turn a string into a function and obtain the result?
2 views (last 30 days)
Show older comments
Answered: Titus Edelhofer on 30 Jul 2018
To be more precise I have a sequence of functions that result in several outputs that all look similar to this:
I tried using str2func but that didn't seem to work.
Titus Edelhofer on 30 Jul 2018
maybe you can describe a little more what you are trying to achieve? Your "function" has no input, so you could basically evaluate the string:
str = '((5/5)+5)+(5*((5*5)-5))';
value = eval(str);
If you indeed have a function, e.g.
str = '2*exp(x)';
then Per's suggestion of using the "@" and str2func works fine:
fcn = str2func(['@(x) ' str]);
More Answers (0)
Find more on Text Files 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!