In this tutorial, you will extract a single column from a 2D array. The function will be saved as a subVI, both for the purposes of this tutorial, but also because it will be needed in a later lab exercise.
Instructions:
- Create an empty VI
- Insert a 2D Numeric Control Array
This will be the input to the function, by it's definition this must be in 2D- Modern -> Array, Matrix & Cluster -> Array
- Add a Numeric Control to the VI and move it inside the array
- Right-click the border of the array and choose: "Add Dimension"
- Rename it "2D Array"
- Add a Numeric Control and rename it "Column Index"
- This is the index of the column to be returned
- Create the output 1D array (the column to be returned):
- Create a copy of the 2D array (Ctrl+C, Ctrl+V (deselect everything when you paste)
- Click on the border of the copy and choose: "Remove Dimension"
- Rename this array to "Column"
- Right-click the numeric control in the array and choose: "Change to Indicator"
- Your Front Panel should now look like this:
- You will now extract the wanted column on the Block Diagram. This will be done with the "Array Subset" function
- Connect the array input to the top left
- This makes the function expand, as your input array is 2D. The top 2 terminals control rows, the bottom two columns. With the second to last, you choose which column you wish to start extracting from (index), the bottom terminal chooses how many columns to extract - in this case 1. Connect "Column index" and a Constant valued 1 to the respective inputs of the function.
- Connect the output array with your 1D Column Array
- This will cause an error!
- Despite having just chosen a single column, the variable is still defined as a 2D array, which cannot be connected to your 1D output array.
- Remove the bad wire (Ctrl + B)
- You will now force the output array to be 1D:
- Use the "Reshape Array" function
- The function also has to count the number of rows in the array, using an "Array Size" function. Insert one on the Block Panel
- The "Array Size" function gives the size in all dimensions, e.g. both the number of rows, but also
- columns. Since we only need the number of rows, we will also need an "Index Array" function. Insert one on the Block Panel
- Connect your "2D Array" with the Array input of "Reshape Array". As mentioned earlier, the function also need the number of rows. You can get this by connecting "2D Array" with the Array input of the "Array Size" function
- However, Array Size will yield both the number of rows and columns. Since we only need the number of rows, the ouput from "Array Size" has to be connected to "Index Array", which can extract a single value from the array. The one needed (the number of rows) is the first, located at position 0 (remember the zero indexing of LabView). Connect a 0 constant with "Index" on the function. This will yield the bumber of rows, which must be connected to "Dimension Size" on the "Reshape Array" function
- Connect the output from "Reshape Array" to the 1D Column Array you created on the Front Panel (titled "Column")
You have now created a VI, which can extract a single column from a 2D array which is ordered into a 1D array. This has to be changed into a subVI, which can be used in other exercises.
- Right-click the icon at the top right corner of the Block Diagram and choose "Edit Icon…"
- Change it into a table with a high-lighted column. You can use the "Glyphs" tab and insert the "Spreadsheet" icon from the "Things" category. Fill one of the columns with a different color, e.g. red, using the "Fill function"
- Press OK
- You will now wire in- and outputs. Right-click the icon in the top right corner of the Front Panel and choose "Show Connector". The default connector has too many in- and outputs.
- Right-click the Connector icon and choose "Patterns". Choose the icon with 2 inputs (boxes on the left) and 1 output (boxes on the right)
- Connect the inputs:
- Click on the top left box and then the 2D array
- Click on the bottom left box and then the Column Index
- Connect the output:
- Click on the right box and then the Column Array
- Save your subVI as "Get Column.vi"
Now you have to test your new VI:
- Open DiceThrows3.vi
- Add a While loop at the bottom, below the loop
- Right-click the Block Diagram and choose "Select a VI…"
- Open your subVI: "Get Column.vi" and click inside the While loop
- Connect the output array from the for loop with the 2D Array input of the "Get Column" subVI
- Right-click the "Column Index" input on the subVI and choose Create -> Control
- Right-click the "Column" output on the subVI and choose Create -> Indicator. Expand this to 12 cells on the Front Panel
- Move "Column Index" and "Column" below the 2D array on the Front Panel
- Set the number of throws to e.g. 10.000 and run your VI
- You can now browse the columns by changing the "Column Index" value.
- Remember, that the first and second columns are empty, as you cannot hit 0 or 1 using two dice.
- Save yoru VI as DiceThrowsColumn.vi