LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to build an array inside a loop

Solved!
Go to solution

Hi everyone,

 

I have a basic question about labview. I would like to create an array in a "for" loop in that way:

inside the loop, if a condition named "A" is verified, then, i add a new value to the first column of the array.  Otherwise, i create a new column, and start to add values to this column when the condition "A" is verified, etc.

 

I put an example of a VI where the condition "A" is "the increment of the loop + 1 is not divisible by 5" and the values of the arrays are the increment of the loop + 1. The loop stops when the increment = 25.

In this example, i have a single column with the values [1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21 22 23 24]

while I would like to have an array of 5 columns:

1   6   11   16   21

2   7   12   17   22

3   8   13   18   23

4   9   14    19   24

 

Note than this is just an example. More generally, the array can contain columns of different lengths.

 

Thanx for your answer

Andrew

 

0 Kudos
Message 1 of 14
(7,440 Views)

You can't have columns in an array have different lengths - you can dynamically increase the size of the array (e.g. build array) but it means you will get the default value of the array in the other columns up to the new size of the array (normally 0).

 

You will need to either keep track of the last index you wrote to for each column so you can trim it later or put an array in a cluster and then create an array of those (an array of a cluster of an array) - that will allow your column data array to have different lengths and you'll just need to unbundle/bundle the array before adding new items.


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 2 of 14
(7,427 Views)

Thank you for your answer! It would be ok if I dynamically increase the size of the array, keep track of the last index and trim it later. Nevertheless, I don't know how to dynamically increase the array in the way presented in my first post? Could you show me that on the attached VI?

0 Kudos
Message 3 of 14
(7,395 Views)

No, because I don't have LabVIEW 2014 installed on my machine. Use the array functions (index/build array/replace array subset) to manipulate the array and add items into the column.


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 4 of 14
(7,372 Views)

Actually, I don't manage to create a new column. I used a Shift Register and the Build Array Function to add values to the first column vector, but I don't know how to create a new column in order to dynamically add values. I tried to use the Replace Array Subset Function but it doesn't work. What's wrong?

0 Kudos
Message 5 of 14
(7,332 Views)

Try to use Insert into an array. It will have to be into a 2D array, wiring the Column element. If you wire a Zero, it will put the data in the first column. Its sounds like you understand shift registers, so you will have to use those to keep data in the 2D array. I don't have 2014 or I would look at your code

0 Kudos
Message 6 of 14
(7,301 Views)

Thanx for your message.I now have the following problem: as I don't know what will be the number of columns, the 2D array is initialized with 2 columns, and then i can't rise the number of columns in the Shift Register since the array is different than the original. I have the same kind of problem if I use the option suggested by Sam_Sharp for the Clusters. For example if I initialize a Cluster with a 1D array, and then i want to add new 1D arrays in the cluster, I have this kind of message: "a cluster of 2 elements conflicts with a cluster of 1 element".

0 Kudos
Message 7 of 14
(7,285 Views)
Solution
Accepted by andrew_ryan

Try looking at this VI I wrote, It takes a 1D array and puts it into a 2D array based on a threshold. I think it is a pretty good example on how insert an array works. I hope it helps

0 Kudos
Message 8 of 14
(7,277 Views)

That's perfect! Your VI showed me how to insert a column into an array. Many thanks for your help!

0 Kudos
Message 9 of 14
(7,272 Views)

I am glad it helped. Just remember that the columns will always have the same number of rows. If you change the values in the threshold on the front panel of that VI, it will show you what I mean. 2D arrays like to be "rectangular".

0 Kudos
Message 10 of 14
(7,265 Views)