LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to initialize a 2-dim array with 5 columns

Hello!

We have a for-loop and we have initialized a 2-dim array. But when we runs the loop we do not get any values in the array. We think it is because the array do not have fixed numbers of columns from the beginning. How can we do this? One should be able to create number of columns and rows dynamically from time to time. Or is it easier to have a fixed number of columns and be able to decide numbers of rows dynamically?

Best regards.
0 Kudos
Message 1 of 11
(3,517 Views)
Hello Ex-jobb,

You can add/remove rows and columns as often as you like in an application. There are several ways of doing it and the values youll find in new Rows/Columns will depend on how you add them.

I ussually only initialise an array (whatever dimension) if I know exactly how big it needs to be from the beginning. But if you do initialise it with a given value, all columns and rows will be filled with that value.

Can you attach a copy of the code (atleast a snippet of the relevant code) you are using? It may help to further understand your problem.

Cheers,

Bazza
0 Kudos
Message 2 of 11
(3,514 Views)
Like Bazza said, attaching your code is probably the best idea. However, since you mentioned a loop, I would like to venture a guess as to the cause. The number of iterations a for loop will have is determined by the smallest index given to it. That means if you auto index into a loop an array with 4 rows, an array with 8 rows and wire 5 into N, the loop will only run 4 times. That means that any arrays being indexed out of the loop will be size 4. In your case, it's possible the loop runs 0 times and so, if the array is indexed out of the loop, it's size will be 0.

___________________
Try to take over the world!
0 Kudos
Message 3 of 11
(3,511 Views)
Hello!

We attache the code. We have changed some details from the origin so you can see only the problem.

Thank you for your help! Best regards!
0 Kudos
Message 4 of 11
(3,506 Views)
Sorry!

Here is the code 🙂
0 Kudos
Message 5 of 11
(3,505 Views)
To say, the problem is how we can initialize the array to have x numbers of columns and x numbers of rows from time to time...

Best regards.
0 Kudos
Message 6 of 11
(3,504 Views)
Hello Ex-jobb,

perhaps this helps...

Best regards,
GerdW
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 7 of 11
(3,495 Views)
Hi Ex-jobb,

Attached is a simple VI showing how to initialise an array with a given amount of columns / rows.

As tst pointed out, a FOR loop will only iterate as many times as the size of the smallest array given to it. In the attached VI I have initialised a 1D array and indexed it into the side of the FOR loop. This loop will run 12 times (array has 12 elements) before finishing. Its the same as wiring 12 to the N terminal of the FOR loop.

The input array ('LIST') that determines how many times your loop iterates is empty by default. Running the VI like this will not allow the loop to iterate even once. This would be the case even if you wired a given number to the N terminal. If you intended the LIST array to be empty on startup you need to disable the indexing (where the array data enters the FOR loop - right click on the tunnel) and index the array inside the loop using index array.

Hope this has helped answer your question?

Bazza
0 Kudos
Message 8 of 11
(3,491 Views)
Hello!

Can one do this dynamically? The problem is that we do not know from the beginning what size the array will be... and the elements in the array should be different from running to running. Thank you for your help! Best regards.
0 Kudos
Message 9 of 11
(3,479 Views)
You could have some code up front to determine the size of the 2D array and wire the values to the input of the 'initialise array' function directly (before you use it in the main part of your code).

Alternatively you could initialise a 2D array to a nominal size and add rows or columns as necessary. The attached VI demonstrates roughly how to do this. I appreciate its out of context of your application but hopefully it will give you enough of an idea on how how to implement it in your code.

NOTE You can also delete rows or columns by using the 'delete from array' instead of the 'insert into array' function.

Regards,

Bazza
0 Kudos
Message 10 of 11
(3,471 Views)