LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble inserting into 2D array

Solved!
Go to solution

Hello,

I'm trying to add an element to the end of row 0 of a 2D array (5 times).

Then add an element to the end of row 1 of this 2D array (5 times). etc...

I'm getting the row of interest with index array, then append my new element with build array and then use replace array subset to put the row back in the 2D array. 

However, I end up with an array that is 25x1 instead of 5x5.

It looks like LV 2012 supports replacing a single element by wiring to the row and col inputs but I'm currently running LV2010.

The attached .jpg is a snippet of the original code.

The attached .vi is a sample I'm playing with to try to figure out what I'm doing wrong.

 

Any help is greatly appreciated.

 

 

Download All
0 Kudos
Message 1 of 7
(3,118 Views)
Any thoughts on this? I've been stuck on this problem for 2 days with a deadline fast approaching. I think LV 2012 might offer a different way to address elements in the array but I worry about upgrading in the middle of a project.
0 Kudos
Message 2 of 7
(3,072 Views)

like this?ArrayBuilderTest.png

0 Kudos
Message 3 of 7
(3,065 Views)
Solution
Accepted by topic author ebloohm

Hi eblohm,

 

there are two fundamental problems with your VI:

1) An array will always be 'rectangular'. As soon as you add an element to one row all other rows with also get an element (set to default values) added...

2) ReplaceArraySubset will only replace existing elements! You're trying to replace non-existing elements (aka build up your array), that will not work.

Did you even try to debug your VI with highlighting switched on?

 

Possible solution:

Define your array with the final size! Then use ReplaceArraySubset to replace single elements in your array!

 

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 7
(3,037 Views)

Thanks.  That's helpful.

 

I did analyze with highlight execution on.

The array size was always 0.  Or, if trying it a different way, I'd only populate the first element of each sub-array.

I couldn't figure out why but you've explained that.  Thanks.

 

I don't know the final size and even worse, each sub-array will be a different size.

 

I guess I'll need to come up with a different data structure.

 

Thank for the help,

Erik

0 Kudos
Message 5 of 7
(3,029 Views)

Hi Erik,

 

when your (1D) subarrays will be of different length you should use an array of cluster of 1D array...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 7
(3,024 Views)

GerdW is right. Your best option is probably an array of clusters of 1D array of clusters.

 

The first thing to do, if you have not done so already, is to make a typedef of the inner cluster - the one with the actual data values.  The reason for making this a typedef is that if you ever need to change it, you only need to change the typedef and the changes will propagate through the entire project.

 

Next make a 1D array of the typedef cluster.  This will be the datatype for your varaible length arrays.

 

Put this array in a different cluster.  You now have a cluster containing an array of the typedef clusters.

 

Now make an array of this cluster.  This is your final array. This is a 1D array, the elements of which are the cluster containing the array. The nice thing about this for your application is that this final array thinks it contains clusters. It does not care that each element can be a cluster containing an array of a different length than the array in the cluster in a different element.

 

It is tricky to describe using words, but easy to implement in LV. 

 


 

Even though you do not know the maximum size of the array, it is still much better to initialize the array and use Replace Array Subset.  You can probably estimate the maximum size based on how long the program will run, how fast elements will be added, or the maximum amount of memory you have available. After the protion of the program where elements are put into the array ends, remove the unused portion of the intialized array.

 

Lynn

0 Kudos
Message 7 of 7
(3,011 Views)