How can I turn an equation into a string for input?
Show older comments
A function I am trying to use requires the function input to be a string. For example taking the equation "f(x)=x^2 - x + 5" as an input, but needing to be a string.
I am unsure of how to turn this into a string that the function will then accept (uses feval). All the questions I've found from researching use "str2sym" but I don't believe this is doing what I need.
Thanks in advance for your help.
2 Comments
Johannes Fischer
on 29 Aug 2019
How exactly is the function defined in your code? "f(x)=x^2 - x + 5" already looks like a string to me.
Thomas Smith
on 29 Aug 2019
Answers (1)
darova
on 29 Aug 2019
str2func should work:
s = input('Enter the function:\n');
% Enter the function:
% '@(a,x) a*sin(x) - 1/2'
f = str2func(s);
Dont forget quotes
Categories
Find more on Characters and Strings 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!