LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Why no unwired cases?

I'm trying to build an array using only a subset of the elements in an input array.  I'm trying to use a case structure to select elements from the input array that meet certain criteria and place them in the output array.  The problem is that when the value in the input array doesn't meet the criteria, the default value gets placed in the output array.  How do I prevent this from happening?  It seems to me that in LabView there is no way to have an "if" statement without an accompanying "else" statement although this is possible in every other language...
0 Kudos
Message 1 of 5
(3,003 Views)
The answer is:
1. Don't select "Use default if unwired" on the output (right side) of the case structure.
2. On the "default case" wire the input directly to the output.
0 Kudos
Message 2 of 5
(3,001 Views)
That doesn't work because the input to the case structure is an array and the output is an element of the array so they cannot be wired together.
0 Kudos
Message 3 of 5
(2,993 Views)
Would you be able to place a copy of your vi on the thread for us to see?
0 Kudos
Message 4 of 5
(2,987 Views)

@Qurple wrote:
That doesn't work because the input to the case structure is an array and the output is an element of the array so they cannot be wired together.


Hi Qurple,

If your loop needs to output an array, then that's the kind of data you'll need to keep track of in your loop.  The best way to do this in LabVIEW is with a shift register.  It's not as simple as allowing an "if" statement, since with a dataflow language like LabVIEW, you must define a value to "flow" through the wires and tunnels for every possible case of code execution.  So in your situation, the easiest way to do this is to have a shift register that keeps track of the array subset you want to output, and you can add/remove items to this array, or leave it alone.  Here is a screenshot showing two approaches:

Note that there aren't actually two separate case structures in the loops, I'm just showing you the contents of both cases to help in your understanding.  In approach #1, we start with an empty array, and add new elements to it (with the Build Array function) whenever a condition is met (in my case, I'm simply adding all elements greater than 2).  In approach #2, we actually start with the entire array, and remove elements from it (with the Delete from Array function).  Note that approach #2 is a bit more tricky, since we must keep track of the index into the array where we are when we're looking at a value to see if it needs to be deleted.  Approach #1 is by far the most common approach to this problem, although Approach #2 would be more efficient if your output array is going to contain most of the elements from your input array.

I hope this helps you understand the issue a little more clearly.  I remember when I first learned LabVIEW, shift registers were one of the more difficult concepts for me to grasp.  But they are truly essential for just about any conditional coding you find yourself doing.

Good luck,
-D

Message Edited by Darren on 10-18-2007 11:04 PM

Message 5 of 5
(2,969 Views)