VI High 26: How to Use the Index Array Function in LabVIEW
This episode is part of a VI High series focusing on arrays that runs from VI High 24-32.
In VI High 25 we were manipulating some 1D and 2D arrays with some simple functions. Now we’re looking at one of the most common array functions: Index Array. How do I grab a single element out of a 1D array? Or a row/column out of a 2D array? And what is array polymorphism? It’s time to take a couple minutes and find out.
For more on programming in LabVIEW, check out the Sixclear Lucid LabVIEW Fundamentals Training (formerly Sixclear LabVIEW Fundamentals) course at www.sixclear.com/labviewtraining/.
Experience level: Basic
One of the most valuable is the Index Array function. I’ll put it down here. Why is it valuable? Well, let’s say in my code, I want to get ahold of this element and use it, maybe to multiply by another value. How do I get that out of that array?
Well, that’s the Index Array function. I take my array, wire it into the Array Input, and below I see the Index Input. Create a constant, and what number am I going to put in here? Remember that array is zero indexed, so zero, one, two. I’ll put in a two. Create an output. Run it, and indeed, there’s my value.
What happens if I take that same index array and wire a 2D array up to it? Look what happened: it changed. The context help can tell me what I’m seeing. It says it’ll return the element or subarray of a n-dimensional array. Hmm. Well, what if I were to go and put a zero in the top input?
You see I have Index Row, and Index Column. It says disabled, but that’s only because I don’t have anything wired into right now. So I’ll create a constant, and I’ll also make this two. Create an indicator, and I get a subarray out. Let’s take this subarray and pull it over here, run the VI, expand it over, and as we can see, what I’ve pulled out is this row - zero, first, second row -right here. Could I have also pulled out a column? Absolutely. I would have just wired this into the lower input.
And now look at the tip strip says Index Column, and then the row index is disabled. I run it, and indeed now I’m looking at the second column, right here. Can I go and just pull out one element from here? You know it. I’ll pull this down, hold down CTRL, click and drag to create a copy. LabVIEW auto-wires for me, and it also breaks a wire. Why does it do that?
Well, by wiring both inputs, I’ve essentially told the function that I’m going to specify both the row and column so that it should output a scalar value instead of an array. So I’ll get rid of this, and create an indicator. Let’s change this value to three, run it, so the third row down - zero, one, two, thee - and the second column over - zero, one, two - and there it is.