LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a Dynamic Array

I've been trying to come up with a way to make a dynamic array in LV 6.i so that it's always the exact size to match the data. The way I do it now is to just initialize a huge array like 1000x2 and set everything to zero. Then I use Replace Array to fill in as needed and leave the rest at 0. This works, but takes a long time to execute, isn't guaranteed to be able to manage every data point, and leaves a "ghost plot" at the initial value for the left-over 0's. I'm also not really a pro at using clusters, so if there is a way to do it and keep everything as an array, that'd be ideal. Even better yet, if someone already has a sample .vi made that does this and wants to attach it, by all means...
Thanks
p.s.
Thanks to everyon
e who answers questions on here and does so, so quickly. You make this site an invaluable resource.
0 Kudos
Message 1 of 8
(3,907 Views)
sorry for the multiple posts... computer was acting up
0 Kudos
Message 2 of 8
(3,907 Views)
I'm not sure of what you want exactly... A simple solution could be to use the function "Insert into Array", instead of "replace into array", this way, the size of the array automaticaly increases to fit the new data.
In the example provided, I'm using a shift register, with an empty array as the initial value. You can chose how much points you want to had (I guess in your case you just receive the data, but this is for demo purpose...), and you will see the array expand accordingly.

I hope this helps, if not, please be a little bit specific 😉

oh, yeah, sorry for the cluster, you can get ride of it, it was just more convenient for me...

-julien
0 Kudos
Message 3 of 8
(3,907 Views)
If you want to build an array with an unknown number of components, a shift register is what you need. Right click on the edge of your while loop and select add shift register, then use build array with two (or more) inputs, choose the first input as an array and wire it to the left side of the shift register and the output to the right. Wire your data to the element input (this is supposed to change in every loop) and... Done! If you initialize the shift register with an empty array, when you exit the loop you'll have an array with the number of components you want.
Hope this helps
0 Kudos
Message 4 of 8
(3,908 Views)

Hi,

I was reviewing a post that you replied to.  This post dates back to 2001 so bear with me.

I was also wanting to create a dynamic array.  I downloaded the example from previous post to this original message.  The example worked great in my application but I still don't understand. 

If the Insert Into Array has an Index terminal, how does adding shift registers make the array grow.  Why not wire the shift registers to the index terminal.  I see that it doesn't work but I don't know why.

0 Kudos
Message 5 of 8
(3,702 Views)
Hello Jim,
 
The insert into array function is what is making the array grow.  In this case by adding the new elements to the end.  If you wire a number to the and index, then it inserts the new elements at that point of the array.
 
The purpose of the shift register is to maintain the array from one iteration of the loop to the next.
If  you tried adding the shift registers to the index, it would break the VI because you would be putting an array into an input that is looking for an integer number.
0 Kudos
Message 6 of 8
(3,688 Views)
The shift register contains the data in the array. If you insert a new element, you want to pass the new array back to the Insert Element function so that you can add a new element and keep the old. For example, the first time through you add some data to element 0 (the index). The second time through, you add data to element 1, third time through, data to element 2, etc. The data can be anything (strings, dbls, Booleans) and the index to an array is an integer. Think of an index to a row in a single column spreadsheet and each row can have different values.
0 Kudos
Message 7 of 8
(3,686 Views)


@Jim P wrote:

If the Insert Into Array has an Index terminal, how does adding shift registers make the array grow.  Why not wire the shift registers to the index terminal.  I see that it doesn't work but I don't know why.


You are absolutely right that proper code should have a shift register that keeps track of the insert point and gets incremented every time the array grows. However "Insert into array" has a special meaning if the index is NOT wired: it appends the new element to the end of the array.

Quote from the online help:


"If you do not wire any index inputs, the function appends the new element or subarray to the end of the n-dim array."

This defined behavior is unfortunately rather arbitrary and not very intuitive. Fortunately, you never need to use it! (and I never do!) 🙂 The same operation can be done with a simple "build array" node, giving code that is much more obvious by simply looking at the diagram (see image). There is no difference in the result.


 

Message Edited by altenbach on 01-31-2007 01:52 PM

0 Kudos
Message 8 of 8
(3,680 Views)