From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Parse formula in Labview

I have a question. How to parse in Labview text string contains one formula and then use it with variables, functions, dlls etc. Iuse "Eval parse formula" function, but a want add to that my own functions like: "Pcalc[a;b;c;..]name". And I will use a, b, c variables, and "name" of function.

I did try that a few times, but can't do it.

0 Kudos
Message 1 of 22
(6,089 Views)

We are mostly graphical programmers here, so I would like to ask "why???".

 


@0Forest0 wrote:

... and then use it with variables, functions, dlls etc..

How exactly are you imagining that to work? What do you mean by "use it"?

 

Have you tried mathscript? LabPythonPython Integration toolkit? Gpower expression parser?

 

(Disclaimer: I am not really familiar with any of those)

Message 2 of 22
(6,061 Views)

Have you tried mathscript? LabPythonPython Integration toolkit? Gpower expression parser?

 Exactly are you imagining that to work? What do you mean by "use it"?

 

No. I mean use only in Labview. User enter the formula and variables. Program parse the string and calculate results with that variables in a loop.

0 Kudos
Message 3 of 22
(6,020 Views)

@0Forest0 wrote:

Have you tried mathscript? LabPythonPython Integration toolkit? Gpower expression parser?

 Exactly are you imagining that to work? What do you mean by "use it"?

 

No. I mean use only in Labview. User enter the formula and variables. Program parse the string and calculate results with that variables in a loop.


Google "LabVIEW parse formula".  I think you'll find some stuff you can use.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 4 of 22
(6,006 Views)

No, you cannot declare and use a function in Eval parse formula. You'll need Mathscript for that (and MatLab). You can 'declare functions' by creating sub-vi's which you can call by parsing some text.

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 5 of 22
(5,993 Views)

I've made this, but it's customer proprietary.

 

It has it's uses, for instance, when you want to define a calculated channel based on used input (a formula). The custom functions make sense, an interpolate, PID, LUT function for example can be very convenient.

 

My solution was to use Dykstra's Shunting-yard algorithm. An absolute peace of beauty. And in principle very easy.

 

In formula parsing, there are some tricky parts, especially with ^ (something the eval from NI did\does wrong). 2^2^2^2 = 2^(2^(2^2)) = 65536, not ((2^2)^2)^2 = 256. ^ is the only right associative operator, and that is often overlooked.

 

I wanted to allow math on scalars mixed with arrays, scalars vs scalars, arrays to arrays and Booleans vs Booleans, etc. since invoking a formula on two arrays one element at a time is dreadfully slow. This is just a complexity. If you don't need that, you can do it in a few days.

Message 6 of 22
(5,976 Views)

My solution was to use Dykstra's Shunting-yard algorithm.


It is may help. I'll look it up, when I have a time.

0 Kudos
Message 7 of 22
(5,943 Views)

I also have done something like this for purposes of a calculated channel.  My method was:

 

I used a variant attributes lookup table to store all of my current channel values by name... and then used [ ] around the channel name that was to be used as a variable.  For example

 

[Temperature] * 1.8 + 32

 

I then put the formula string through a look with a regex looking for [ ], and when it found it I would pull that out of the string and replace it with a string value from my lookup table.  I'd then feed this into the parse formula.

 

It won't interact with DLLs or act like a function like you had specified... but it will allow you to parse arbitrary text strings as formulas with variables on the fly.

Message 8 of 22
(5,925 Views)

wiebe@CARYA wrote:

 

In formula parsing, there are some tricky parts, especially with ^ (something the eval from NI did\does wrong). 2^2^2^2 = 2^(2^(2^2)) = 65536, not ((2^2)^2)^2 = 256. ^ is the only right associative operator, and that is often overlooked.

 


 

Excel and Matlab doing it also wrong (details).

 

However, the LabVIEW help says that exponentiation is right associative and some simple testing shows it to work correctly, except for the formula parsing. For example the Formula express VI and expression node give the correct result for right-association. (See image)

 

 precedence.png

 

Obviously, the sentence (link)  "The precedence of operators is the same for the Mathematics VIs as in Formula Nodes. Refer to Formula Parsing VIs for more information about specific Formula Parsing VIs." is incorrect for exponentiation.

 

(I have not investigated in details, but that should be documented and corrected).

Message 9 of 22
(5,918 Views)

My solution to this problem was to use muparser: http://beltoforion.de/article.php?a=muparser

You can add custom functions and operators if needed.

 

I posted an API for LabVIEW here: https://lavag.org/files/file/295-lv-muparser/

0 Kudos
Message 10 of 22
(5,250 Views)