VI High 27: Array Polymorphism and How to Use the Build 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 and 26 we started manipulating some 1D and 2D arrays with the Array Size, Add Array Elements, and Index Array functions. Now we’re going to take a look at array polymorphism and then the Build Array function. How do I combine two arrays in LabVIEW? How do I concatenate them? Watch and learn!
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
Now we’ve seen these functions change based on what we wire into them. Why do they do that? Well, that’s called polymorphism. That’s right, polymorphism. And if you take the CLAD exam, you’ll see it on there. Let me be explicit. Polymorphism. Look at how important that is.
It’s just the ability of the functions to change their functionality based on the inputs that are wiring in. Go ahead and look it up in the LabVIEW help, or check out the Sixclear Lucid LabVIEW Fundamentals Training course. We talk about it there. Anything else valuable in that array palette? Oh yeah, there’s a ton. A lot of it follows the same patterns that we’ve already seen with the Index Array, manipulating the rows and columns, changing the behavior based on what we wire in, so we won’t go into every one of these. The context help is pretty valuable.
But building an array is pretty common. I can pull that down here, and I can create a new array from a variety of things. So let’s say I want to combine these two arrays. To do that, I need to expose multiple elements, so I’ll hover over the bottom, click and drag, two elements, wire this 2D array into the top, wire this 1D array into the bottom.
What comes out? Well, we create an indicator, pull it down here, expand it a bunch, run, and we see these two arrays have been combined together, with this array being added to the bottom of the existing array coming from here.
Now what if I wired in two 1D arrays? Would it do the same thing? Well, it depends. Let’s get a little crazy and create a copy of this, take this array right here, which we know is this array, and then, take this array and wire it in. Create an indicator, and you see our resulting array is actually a one dimensional array.
Of what? Well, I’ll run it, and I’ll see the first two elements from here, and then all the rest of the elements from here. So what I’ve really done is concatenate the array. Take two 1D arrays, and put them end to end. In fact, if I right click on it, I see concatenate inputs is checked, whereas down here I see it’s not. It’s not even an option, because you can’t concatenate arrays of different dimensions.
But can I create a higher dimension array from this build array function? Yes I can. I can right click on it, uncheck concatenate inputs. Now, again broken wire because this wants to create a 2D array. I’ll delete that, create an indicator, there we are. Let’s get rid of some of this other stuff. CTRL-B to clean up the broken wires, run it, and there we go. The two 1D arrays have formed a 2D array. As you see, LabVIEW padded this first array with zeroes, because it wasn’t the same size. But that’s ok.
Well, that was really fun. What will we do next? Well, next episode we’ll go about creating arrays in LabVIEW where we haven’t already manually created them on the front panel or block diagram. So we’ll use the Initialize Array function, which is pretty easy, but we’ll also do some auto-indexing through loops. So, see you next time.