LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Default value output of for loop N = 0

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

0 Kudos
Message 1 of 19
(6,344 Views)

Hi Bokor,

 

that is explained in the beginner tutorials - and hidden somewhere in the help.

 

To have ALWAYS a valid output of your FOR loop you should use a shift register with an initialization value wired…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 19
(6,332 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 3 of 19
(6,316 Views)

Where is this documented in LabVIEW?

 

 

default.jpg

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 4 of 19
(6,307 Views)

@aputman wrote:

Where is this documented in LabVIEW?

 

 

default.jpg


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.

 

0 Kudos
Message 5 of 19
(6,284 Views)

Smiley Frustrated  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?

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 6 of 19
(6,268 Views)

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

0 Kudos
Message 7 of 19
(6,222 Views)

Hi Peter,

 

you could call your local NI support and ask them about this very topic (do you have a SSP contract?). Then post the answer here!

 

Sometimes the NI support should do it's work… 😄

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 8 of 19
(6,220 Views)

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.

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 9 of 19
(6,199 Views)

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!

0 Kudos
Message 10 of 19
(6,188 Views)