Alternative to eval('equation in string form')
1 view (last 30 days)
Show older comments
I want to know how to avoid eval to solve an equation or expression that is in string form . For eg.if the equation is 'abs(Array1) + min(Single value1) .* log(Array2)' ie in string format . Using eval('equation in string form') does work most of the times . Except when the sizes of abs(Array1) & log(Array2) are different . So is there any way to divide the equation , solve them one by one, check their sizes (interpolate if sizes are different) and then proceed further with the equation ?
This equation is just an example as it can have different forms/function even nestec functions ie functions within functions. Also though i am making the sizes of arrays like Array1,Array2 same before passing them to eval but sometimes the output from functions alters their size due to which further operations are not possible in eval.
3 Comments
Guillaume
on 5 Apr 2017
In my opinion, you have two options: use the parser you already have, matlab, which you invoke as you have done with eval. However, you have to abide by the rules of this parser, so the size of whatever you combine must be the same.
If you want to have your own rules, then your second option is to indeed write your own parser, which you can do either in .Net or matlab. Again, this is not trivial. Before you even start writing code, you need to define the whole grammar of your parser.
Answers (1)
Steven Lord
on 5 Apr 2017
Are you performing these computations symbolically?
x = randi(10, [1 5]);
syms Array1 Array2
y = abs(Array1)+min(x).*log(Array2);
If not, can you show a small example of source code that you're currently using to "solve an equation or expression that is in string form"?
2 Comments
See Also
Categories
Find more on Get Started with MATLAB 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!