Alternative to eval('equation in string form')

1 view (last 30 days)
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
sangeet pillai
sangeet pillai on 5 Apr 2017
There is a .net application which calls a matlab dll for faster processing of data. So that application provides the equation and filename from which to get data both in string format.
I have no idea about parser. Do we create it in matlab? Could you please explain in brief about it so that i get a start?
Guillaume
Guillaume on 5 Apr 2017
See parser. As Stephen said it is not a trivial task to write (at least a decent one).
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.

Sign in to comment.

Answers (1)

Steven Lord
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
sangeet pillai
sangeet pillai on 5 Apr 2017
Edited: Stephen23 on 5 Apr 2017
y='abs(Array1)+min(x).*log(Array2)';
{
Remove function names(known),symbols,numbers;
Get only names of variables like Array1,x,Array2;
code which reads Array1,x,Array2 values from file;
}
eval(y)
Walter Roberson
Walter Roberson on 5 Apr 2017
That is not MATLAB code. Block comments in MATLAB use %{ and %}

Sign in to comment.

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!