LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Conditionally add empty or filled array to output of While loop for auto-indexing

I didn't see a similar question already posted...

 

I'd like to use a true/false Case structure to pass either an array with data in it or an empty array to the output of a While loop which has auto-indexing enabled.  So, if True, the array gets passed to the output and appended to the previous loop's output.  If False, then the empty array is appended, leaving the previous loop's output unchanged.

 

I think this could be done with a shift register, but then would that be less CPU/memory efficient?

 

I'm attaching sample code...the upper While loop is what I'd like to do with auto-indexing, but the resulting array isn't right: instead of empty arrays, it gets filled in with zero-filled arrays.  The lower While loop uses a shift register to get the right resulting array, but I think I read that this is a less CPU/memory efficient way to build a large array?

 

Many thanks for your help!

0 Kudos
Message 1 of 2
(2,684 Views)

The best way to create a large array is to do it once, outside the loop.  Incrementing the size of an array inside a loop is very expensive in time and memory.

 

Use Initialize Array outside the loop with the size set to the maximum size you expect for the array.  Then inside the loop use Replace Array Subset to put the data into the array.  You will need a shift register to keep track of the next index. After the loop completes you can remove any unused portions of the array (Delete From Array).

 

I put all your code and the code to do what I described above into a single loop so that all three methods would have the same data.  I made it a for loop so I would know how many iterations it would have.

 

Lynn

 

Array in loop.png

0 Kudos
Message 2 of 2
(2,672 Views)