Alternative to eval('equation in string form')
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Guillaume
el 5 de Abr. de 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.
Respuestas (1)
Steven Lord
el 5 de Abr. de 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 comentarios
Walter Roberson
el 5 de Abr. de 2017
That is not MATLAB code. Block comments in MATLAB use %{ and %}
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!