02-08-2018 01:23 AM
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.
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.
Solved! Go to Solution.
02-08-2018 01:52 AM
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…
02-08-2018 02:11 AM
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 ...
02-08-2018 12:06 PM - edited 02-08-2018 12:11 PM
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.
02-08-2018 12:22 PM
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).
02-08-2018 12:37 PM
@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.
02-08-2018 12:44 PM
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?)
02-08-2018 12:53 PM
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!
02-08-2018 12:54 PM
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.
02-08-2018 12:57 PM
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...