cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

Parse formula in Labview

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 9
(289 Views)

Re: Parse formula in Labview

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 9
(261 Views)

Re: Parse formula in Labview


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 9
(220 Views)

Re: Parse formula in Labview


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 9
(206 Views)

Re: Parse formula in Labview

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! ADDQ VIPM
"Only dead fish swim downstream" - "My life for Kudos!" - "Dumb people repeat old mistakes - smart ones create new ones."
Certified-LabVIEW-Developer
0 Kudos
Message 5 of 9
(193 Views)
Highlighted

Re: Parse formula in Labview

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 9
(176 Views)

Re: Parse formula in Labview


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 9
(143 Views)

Re: Parse formula in Labview

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 9
(125 Views)

Re: Parse formula in Labview


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).

0 Kudos
Message 9 of 9
(118 Views)