04-13-2013 12:40 AM
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.
Solved! Go to Solution.
04-13-2013 10:29 PM
04-13-2013 10:51 PM
like this?
04-14-2013 11:58 AM - edited 04-14-2013 12:00 PM
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!
04-14-2013 12:15 PM
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
04-14-2013 12:21 PM
04-14-2013 02:12 PM
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