10-09-2017
09:42 AM
- last edited on
04-04-2024
02:42 PM
by
Content Cleaner
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.
10-09-2017
10:44 AM
- last edited on
01-28-2025
02:40 PM
by
Content Cleaner
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? LabPython? Python Integration toolkit? Gpower expression parser?
(Disclaimer: I am not really familiar with any of those)
10-09-2017
01:49 PM
- last edited on
01-28-2025
02:40 PM
by
Content Cleaner
Have you tried mathscript? LabPython? Python 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.
10-09-2017
03:50 PM
- last edited on
01-28-2025
02:41 PM
by
Content Cleaner
@0Forest0 wrote:
Have you tried mathscript? LabPython? Python 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.
10-09-2017 06:01 PM
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
10-10-2017 02:38 AM
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.
10-11-2017 11:25 AM
My solution was to use Dykstra's Shunting-yard algorithm.
It is may help. I'll look it up, when I have a time.
10-11-2017 01:17 PM
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.
10-11-2017
02:06 PM
- last edited on
04-04-2024
02:54 PM
by
Content Cleaner
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)
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).
05-18-2018 12:07 PM
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/