VI High 24: The Very Basics of How to Create an Array in LabVIEW
This episode is part of a VI High series focusing on arrays that runs from VI High 24-32.
This is something you learn on day 1 of any LabVIEW course, including our Sixclear Lucid LabVIEW Fundamentals Training (formerly Sixclear LabVIEW Fundamentals) course (learn more at sixclear.com/labviewtraining).
Experience level: Basic
“This VI High episode needs no preamble because everyone loves arrays.
So we’ll see first how to create arrays in LabVIEW and how to manipulate them.
So in LabVIEW, I’ll hit CTRL-N to open up a new VI. And then, I’ll tile them (CTL-T) side by side. Creating arrays is a two-step process. First, we put down an array shell, or array container, and then we populate it.
So, going to my front panel, I’ll right-click. Go to my array, matrix, and cluster palettes. Take an array shell. Put it down. On the block diagram, we can see that we have the array terminal, but it’s black, meaning it’s undefined at present. So it’s an array of what? Well, we need to put something in it.
So I’ll do something very simple, which is just create a numeric control. You see when I grab it, and I hover inside the array, I get these hashings along the edge. I click, and it autosizes around that, and now I’ve created an array of double numerics. Now, if I take my cursor and hover over the lower right, you see I get what looks like a little staircase. I’ll just click-and-drag this down and expose multiple elements in my array. Right now, I see that they’re grayed out because the array is uninitialized.
So, let’s say I come down right here and just put in any number. You see that these elements are no longer gray, and now I have a one-dimensional, five-element array.
Now what’s this right here? This is the array index. It shows the number of the element I’m viewing in the topmost place. So currently I’m viewing the 0-th element of the array. You can see if I increment it, I’m viewing different elements of the array. The 0-th, 1st, 2nd, 3rd, and so on.
So it’s handy, if I get back a massive array from data, and I have say six thousand elements, I can just double-click in here and type in whichever element I’d like to see. Maybe the four thousandth. If I do have that much data, it’s also handy to right-click on the border, go to visible items, and check the vertical scroll bar. Now, right now I don’t see a scroll bar because I don’t have enough elements. But if I come down here and create a few, then you can see that I can scroll through them like that. CTRL-Z to undo that.
So I’ve just created an one-dimensional array of controls. How do I create an array of indicators? Well, it’s pretty easy. I can just right-click on the border of the array and change to indicator. There we go. For now, I’ll leave it as a control, so I’ll do CTRL-Z. Now going back to my array, I have a one-dimensional array. How do I get more than one dimension? Well, maybe I just expand over again to the right, but it doesn’t look like I can do that.
So I have to go up here to the index display and I can hover over the bottom to this double-sided arrow, click-and-drag, and now I have two elements showing up. This means I’ve created a two-dimensional array. I can also right-click directly on the index display and add or remove a dimension right here.
For now, I’ll go click-and-drag this over here, and then hover over the lower right. Click-and-drag over here, and now I’ve exposed multiple elements of that array. So right now, I just have one row to add multiple rows, just as before. I’ll come down here and double-click, and there we go. I’ve initialized the array up to this point. And you see I have six rows and eight columns. That’s a pretty big array. As before, we can change what we’re viewing by manipulating the index display. The top index changes the rows. The bottom index changes the columns we’re viewing.
Now before we go further, I want to point out a common frustration that new LabVIEW users have, which is manipulating arrays with your mouse. In LabVIEW, a little bit of nimbleness with the mouse is recommended. So if I like to move this array around, I have to go and grab on the edge of the entire array right here. These resizing dots help me because they tell me what I’m actually moving or resizing. So if I hover over here, I see the blue resizing dots around the entire array shell. Perfect. I can click-and-drag. There we go.
However, we find that sometimes people want to move the array, and they grab an individual element like this. Then that de-allocates the array, and frustration ensues. Don’t do that. CTRL-Z will bring it back. Let’s move this over a bit.
Another common point of frustration is resizing the array. I’ll get rid of the vertical scroll bar first and reduce the number of rows by one. So if I want to, for instance, expose more elements of the array, I can hover over this blue resizing dot, click-and-drag, and multiple elements of the array occur. That’s good. But if I hover just inside of that, you can see now, I’m changing the size of the elements in the array. That can be frustrating. So just be aware of where you’re pointing this thing.
Now let’s head over to the block diagram and see how to create array constants. Creating an array constant is done pretty much the same way as creating an array of controls. First, I right click, head to the array palette, and put down an array constant shell. There it is as before: black, so it’s undefined. I’ll put something in it.
I’ll just go to my numeric palette, and this time I’ll put down an numeric constant which is an integer. Put that inside, and as before, I’ll hover over the lower right. Click-and-drag to expose multiple elements, put a value in to initialize the array, and expand the array elements just a bit so I can see them. Again as before, I go to the index and expand it to create a multi-dimension array, and then expand over to the right. Perfect.
Array constants are pretty handy, and we’ll see that they’re valuable for initializing files, architectures, providing dummy data types to certain functions to tell them what to output, and so on. There’s also a nice shortcut to create an array of constants if you have a comparable front panel array of indicators or controls. I would just go click-and-drag this over here, and now I’ve created an array of constants. It’s a copy of this. It’s not linked to it anymore. This is the terminal for that array. This is just a copy in constant format. So I’ll move it over here as before, drag it out, and you see the elements that I’ve already inserted. Likewise, I can go and take this array of constants, pull it over here, and I have an array of controls now.
We’re done for this time around, but next time we’re going to see how to programmatically manipulate these arrays. Because we’ve created them, but we really haven’t done anything. So stay tuned.