LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a separate new arrays for each loop

I have a question about creating new arrays. For example : In my for loop, the initial value is 1 and the final value is 4 and  each iteration increment by 1, meaning that in this case i have 4 iterations. At the same time, i want to create  4 separate arrays called array 1 for first iteration, array 2 for the second iteration and so on, that depends on the number of iteration. Could you give me some advice please?
0 Kudos
Message 1 of 12
(4,376 Views)

Hi Ann,

You can accomplish this with a Case Structure and 4 shift registers.  Make cases for iterations 0, 1, 2, and 3.  Add a shift register to the For Loop for each iteration as well, and initialize each shift register with an empty array.  In the case for iteration 0, use a Build Array to add whatever you want to the array in the first shift register, while just passing through the wires for the other three shift registers.  Then in the case for iteration 1, add whatever you want to that shift register, while wiring through the shift registers for 0, 2, and 3. Repeat this for all cases.

Try it out, let me know if you need any additional advice.

-D

0 Kudos
Message 2 of 12
(4,376 Views)

Hi Daren,

But what if the loop iteration is very big like 50 iterations ? In this case, isn't it very troublesome to add 50 shift register ? Is there  any other methods that you can suggest ? Thank you

0 Kudos
Message 3 of 12
(4,368 Views)

Hi Darren,

But what if the loop iteration is very big like 50 iterations ? In this case, isn't it very troublesome to add 50 shift register ? Is there  any other methods that you can suggest ? Also, what if the number of the iterations is to  be determined by the user ? Thank you for your advice

0 Kudos
Message 4 of 12
(4,363 Views)

Hi Ann,

Ok, the way your first post was worded, it sounded like your specific use case was a hard-coded iteration count of four.  In the case of an arbitrary number of iterations, you would probably want to create a single shift register that contains an array of clusters of an array.  You can't have an array of arrays in LabVIEW, so what I'm suggesting is the next best thing.  Every iteration of the loop, you can use the Build Array function along with a Bundle function to add a cluster of an array to the shift register on every iteration of the loop.  Let me know if you need any more assistance.

-D

0 Kudos
Message 5 of 12
(4,354 Views)
Hello Ann,

you can use kind of an 'array of arrays': just make the output array one more dimension bigger than your arrays 1, 2,.. Then you just use 'Index Array' to select the needed array.
But be aware of problems, if your 'subarrays' will have different sizes! (That's what Darren meant with 'You can't have an array of arrays in LabVIEW'.)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 12
(4,349 Views)
Hi Darren,
 
Your suggestion is what I want. However, when i try an example, it doesn't come out with the result that i want. Maybe I did something wrong. Basically, I have a 2D array with some default value in it. So if the number of loops is 2, it suppose to have 2 separate array with the same set of elements. However, I only had one array with growing set of elements. To illustrate, it suppose to be
Array1    Array 2  
| 1 2 |     | 1 2 |                                        |12|
| 3 4 |     | 3 4 |         rather than              |34|
| 5 6 |     | 5 6 |                                         | 56|                
                                                               |12|
                                                               |34|
                                                               |56|
 
 
I have attached the file. Could you please show me where the mistake is ?
0 Kudos
Message 7 of 12
(4,334 Views)
Hi Ann,
 
The bundle needs to take place inside the loop...see this screenshot:
 
 
As you continue to use LabVIEW more, you'll find that this trick of bundling arrays in order to simulate having an array of arrays is very useful for all sorts of applications.
 
-D

Message Edited by Darren on 06-19-2006 10:36 AM

Message 8 of 12
(4,325 Views)

Hi Darren,

Thank you for your solution. One more question, when you initialized the left shift register, how did you get/create the 1D array of cluster of 2D array ? I am using Labview7.0 , is it some extra function from newer edition ?

0 Kudos
Message 9 of 12
(4,315 Views)

Nope, nothing special.  Just drop an array constant.  Then drop a cluster constant inside it.  Then drop an array constant inside the cluster, then drop a numeric constant inside the array.  It's just that easy!  😛

-D

P.S. - Don't forget to right-click the numeric constant and change its representation from I32 to DBL.

Message 10 of 12
(4,309 Views)