Alternative to eval('equation in string form')

조회 수: 1 (최근 30일)
sangeet pillai
sangeet pillai 2017년 4월 5일
댓글: Walter Roberson 2017년 4월 5일
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
sangeet pillai
sangeet pillai 2017년 4월 5일
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 2017년 4월 5일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2017년 4월 5일
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
sangeet pillai
sangeet pillai 2017년 4월 5일
편집: Stephen23 2017년 4월 5일
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 2017년 4월 5일
That is not MATLAB code. Block comments in MATLAB use %{ and %}

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by