Showing results for 
Search instead for 
Did you mean: 

Passing array through a For loop, and terminating on error or True condition

Go to solution

I'm finding the LabVIEW help a little obtuse sometimes and I've searched but can't find an answer to these two questions (bad search terms maybe??).  Anyway, I have a VI which is used to remove a specific entry from an array and pass the new array out:


Screenshot 2023-09-30 at 18.46.45.png

In this VI CommandDirector has an Array of Commands and I want to remove an entry and store the updated array back in the CommandDirector.  So I'm extracting the array and auto-indexing the input to a For loop.  For each Command I read it's ID (the icon in the For loop) and compare it to a passed Command ID and if equal, remove it from the array.  This should terminate the loop early because it has been found (there is only ever one potential match).  The array is passed out of the for Case and For loop and used to store back into CommandDirector.  The False condition for the Case just passes the array of Commands straight through.  My questions:

1) The Tunnel Mode for the For Loop is "Last Value".  Given that the array either never changes (no match found) or has one entry removed and then the loop terminated, it feels like this is the right setting?

2) I want to stop the loop early when a match is found OR an error occurs.  Have I done this correctly?  I'm not clear on whether the error input when NO error has occurred is treating an empty Error cluster as False or not.

0 Kudos
Message 1 of 10

To answer (1) myself:


Yes it is the right setting.  

0 Kudos
Message 2 of 10

I am not familiar with your tools and what the possible errors are.


Since the loop exits on error, you don't really need the shift register for the error wire.


Is the "removed command"indicator connected to the connector pane? It would be better to have it on the top level diagram after the loop. It is never a good idea to bury indicators deep inside structures, it might even show stale values if the case never occurs. Same for the "removed" Boolean. It should be connected to the wire going to the stop terminal via a last value tunnel.


Sorry posting by phone. I can look at it later once you post the actual vi. Pictures are ambiguous (we cannot see what's in other cases, we cannot tell what's on the connector pane, we cannot see vi settings, etc etc)

0 Kudos
Message 3 of 10

I hate working with "pictures of LabVIEW code", as that's all I can see (and not always clearly).  So I see elements that "look like" DAQmx Read or Write functions, but that's probably just a "misleading icon" ...


But getting back to the Question at hand, I'd suggest the following:

  1. Start with your extracted "Array of Commands" and the "command to be removed".
  2. Write a sub-VI that returns the index of the "command to be removed", returning -1 if there is nothing to remove.
  3. Note that the sub-VI needs to return -1 if, in fact, there is nothing to remove.  I'll leave that detail to you.
  4. Now all you have to do is take your original Array and the Index-to-remove into a Case Statement.  If the index is -1, you pass the Array out unaltered, otherwise you remove the indicated command and return the "diminished" array.  Very simple logic.

Bob Schor


0 Kudos
Message 4 of 10

I only posted an image because it really is as simple as it looks: The CommandDirector is a class which has a property which is an array of Commands.  The icons for reading/writing might be a bit confusing, I'm just picking glyphs that make sense to me: they just return/store an array into that property.


I'll take on board what you guys say and have a look at restructuring, thanks.  


I'd still be interested in understanding how the Error Cluster is interpreted by the OR comparison though.

0 Kudos
Message 5 of 10
Accepted by topic author andrewDJ

When you feed an error cluster to any native boolean terminal that accepts it, it will evaluate according to the boolean element of the error cluster.   In other words, True when there IS an error, False when there is not (and that includes "warnings", where the boolean is False but there may be a descriptor string and a non-zero numeric code).



-Kevin P

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 6 of 10

Thanks Kevin.  I've been used to anything that isn't zero or null being True and I know that LabVIEW is different.

0 Kudos
Message 7 of 10

@andrewDJ wrote:

Thanks Kevin.  I've been used to anything that isn't zero or null being True and I know that LabVIEW is different.

If the code is nonzero and the boolean is FALSE, it's a warning, not an error! For this reason, you cannot use a nonzero error code for your purpose. As has been said, LabVIEW tests the boolean.


Being able to wire an error code directly to a logical function is a more recent code shortcut. In the distant past, we had to unbundle the boolean.




Of course the bottom code is useful if you also want to stop the loop on warnings.... 😄



0 Kudos
Message 8 of 10

I don't remember exactly when this happened, but about 15 years ago (so around LabVIEW 8.6 or 2009, perhaps), NI changed the "Stop" control of For Loops to be able to accept an Error Line as a valid input (so "Stop on Error").  Previously, we all did an Unbundle by Name and wired the "Error" boolean indicator to the Stop, so this was a great improvement in speed and "ease of reading the code".  At the same time, NI made the Boolean functions (such as "And" and "Or") also treat the Error Line as though it was just the "Error" Boolean, so you could easily program "Stop OR Error" to end a While Loop.


Note that you can not simply wire the Error Line to any old Boolean Indicator (such as an LED) -- they are different "Types".  The limited specific exception that NI made 15 years ago for the Error Line is specific for that Data Type (if you look at the Error In on a Front Panel, note that the Status symbol is "special", not available on the Boolean Palette) -- NI does some "magic behind the scenes" to provide this additional functionality to LabVIEW users.


Bob Schor

0 Kudos
Message 9 of 10

Thanks both for the useful information.

0 Kudos
Message 10 of 10