From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Mathscript node executes the whole code all over again despite having an if cycle

Solved!
Go to solution

Hi there,

 

I recently noticed an unusual behavior of a Mathscript node in Labview. In Matlab for instance (and in the majority of other programming languages) you initialize variables before the if the cycle in the code (let's assume there is only one cycle). And then when the cycle condition is fulfilled, these variables will be inside the if loop to undergo some operations. The code won't pick up the initial values of the variables that were specified outside of the loop (in the beginning). But will continue to execute the cycle while the if condition is fulfilled.
But not in Mathscript. For instance the code in the pics.

 

loop_1.PNGloop_2.PNG

 

 

Even when the if the cycle is fulfilled, Labview will run the whole code from the beginning. Hence will write -7 to "c" and only after that will execute the if cycle and "c" becomes 0. Whereas in Matlab under these conditions "c" always will be zero (and it is just logical).
Of course, this code is only one example, but I need to know this crazy logic to troubleshoot my main code. Am I doing smth wrong here, this is the way how the node works? Thank you in advance.

0 Kudos
Message 1 of 12
(7,523 Views)

Hi Michael,

 

Labview will run the whole code from the beginning.

Do you think LabVIEW should NOT execute line 1 of the MathScript because of the IF-statement in line 2-4?

I think even Matlab will execute line 1 before line 2…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 12
(7,509 Views)

I see nothing unexpected. .. scripted languade... one line after the other... 

I you want to avoid the execution of code .. how about a if then else  or case ?

 

Oh, you want the compiler to be smarter than you 😉 

compiler: that user is telling me do do this, but later he maybe don't need it. Lets change the if clause to a if than else ...

 

 

 

Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'


0 Kudos
Message 3 of 12
(7,503 Views)

Hi GerdW,

Matlab will execute line 1 before line 2. But until the if cycle condition is fulfilled, and after that all the lines prior the cycle won't be executed. I did not say anything that it should NOT ever execute line 1. 

 

All these lines before the if cycle are used to initialize variables. And after the code did some operations with them in the if cycle (they got updated values), for sure you don't want to run the first line, erase the updated values and assign the initial ones?  

 

@Henrik_Volkers

Refer to my reply to GerdW. I don't see anything unexpected when the code executes without the fulfilled if cycle condition. Unexpected happens after the if cycle kicks in.

Feels like you are trying to be smarter than me 😉 

If else structures will do, but think about a few hundreds lines of code with stacked if and for cycles that should be re-writen with else structures. Just because the execution in Labview is different from Matlab.

0 Kudos
Message 4 of 12
(7,477 Views)

Are you talking about during the second iteration of your while loop that you are expecting line 1 to not be executed?  If so, that's not the way it's going to work.  You'll need to find some way to do loops inside the mathscript node (if it's even possible). 

 

aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 5 of 12
(7,471 Views)

@aputman

Thanks. Yep, it seems like there are no options. Even with else structures I can see some problems with the first initialization of variables (before if cycles). Thank you for your time.

0 Kudos
Message 6 of 12
(7,467 Views)

Is there even a reason for a mathscript structure?  (Is this actually a simplified example of something so complicated that it can't be easily translated into native LabVIEW code?)

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 7 of 12
(7,464 Views)

Hi Michael,

 

All these lines before the if cycle are used to initialize variables.

In my opinion you are doing some simple math here.

Yes, you "initialize" the variable c, but at the same time you are doing an ADD operation.

Why should LabVIEW (or any other programming language) omit this math operation???

 

for sure you don't want to run the first line

Well, but YOU do so by executing the MathScript node inside a WHILE loop again and again!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 12
(7,458 Views)

Hi billko,

 

Well, as I mentioned before "but think about a few hundreds lines of code with stacked if and for cycles that should be re-written with else structures."

 

So yeah, it is possible. It will take a lot of time to do it and then even more to troubleshoot everything (cycles, timing and so on)... And as a normal well-educated human being, I want to check the most logical way first - use of the designed earlier m-file.

0 Kudos
Message 9 of 12
(7,456 Views)

Hi GerdW,

 

So are there any options for running the first line only once (let's say outside of the while loop), and if cycles inside the loop? I am more than happy to divide this code into a few or maybe combine it with purely Labview structures without Mathscript...

0 Kudos
Message 10 of 12
(7,455 Views)