04-15-2015 11:33 AM
How do I know what the last value in a loop in tunnel mode is if the loop is not executed (N=0)?
In the attached example, Boolean is False if N=0 and True if N>0. I know because I tried.
Where is this documented in LabVIEW? Can I change the default behaviour?
Thanks,
Peter
04-15-2015 11:41 AM
04-15-2015 12:11 PM
If N is zero, then the loop run 0 times. So what would you expect for the output tunnels? The compiler just uses the default values for the data type. For booleans, that is FALSE.
Now this can really bite you if you are using references. The solution is to just use a shift register for the reference (or whatever value you want to provide the default value for). This works since both sides of a shift register are actually the same location in memory. So whatever you initialize the shift register to be will be what will be passed out if the loop runs 0 times.
Also note that While loops do not have this issue since the must run at least once.
04-15-2015 12:38 PM
Where is this documented in LabVIEW?
04-15-2015 01:22 PM - edited 04-15-2015 01:35 PM
@aputman wrote:
Where is this documented in LabVIEW?
That is one confusing help sentence that does not make any sense!! The words "returns an array..." need to distiguish between the value inside the loop and what's returned after the loop. It currently is ambiguous.
Let's assume it talks about what the loop returns after completion (because it also talks about "default data" output).:
Point 1: This is incorrect when the for loop iterates 0 times (e.g. if there is another autoidexing array with zero elements). You'll get empty arrays at the output if you also don't autoindex at the output tunnel.
Point 2: Even if you enable autoindexing you'll get the "default data", i.e. an empty array of that datatype after the loop. So what's the difference???
This help page has been slightly rewritten for LabVIEW 2014 (see here), but does not seem any clearer.
As has been mentioned, use a shift register to pass data across a for loop even if the number of iterations is zero.
04-15-2015 01:50 PM
I didn't even read the whole thing when I posted the image. But you are right....no matter how it is auto-indexed, default data is output when the loop is not run. Is this a feature from the early days of Labview that has since changed but managed to stick around in documentation?
04-16-2015 02:07 AM
Thanks for the shift register hints etc.
Still, do you know where the tunnel/last value default behaviour is written down in an official NI document?
Peter
04-16-2015 02:09 AM
04-16-2015 02:37 AM
Initializing Shift Registers and Using Previous Values. This excerpt from the LV 2010 help talks about using shift registers. The section quoted here disusses the use of uninitialized shift registers:
Use a loop with an uninitialized shift register so that each time the VI runs, the initial input of the shift register is the last value from the previous execution. Leave the input to the left shift register terminal unwired for an uninitialized shift register to preserve state information between subsequent executions of a VI.
Okay, it's there in an Official NI document. I'm sure it's in all the other LV help files as far back as shift registers go. LV 2010 help was the first one that came up in a Google search.
04-16-2015 02:54 AM - edited 04-16-2015 02:55 AM
Formally speaking this help talks about shift registers and not tunnels.
But since the behaviour is clear, I stop beating a dead horse 🙂
And yes, calling the support is also an option. I should find my code somewhere 😉
Have a nice day everybody!