LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to dynamically add to a 2-Dimensional array

Solved!
Go to solution

Hey Guys, I'm trying to dynamically add to a 2-D array. This array will eventually be nx6. We'll suppose it's a 3x2 for now. How do I go about doing this? 1) initialize a 3x2 array of 0's outside the while loop 2) Add a 1x2 array to this array as the 'next row' How do I convert 2 doubles into a 1x2 array? The build array only concatenates the doubles into a 2x1 array. Would I use insert into array? This function only takes in 1 number in the index input. Can I reference using [i,j]? After I add the 1x2 array, I want to use the shift register to keep growing the array. Any thoughts?

0 Kudos
Message 1 of 16
(9,446 Views)

Rather than Insert Into Array, use Replace Array Subset with an array initialized outside the loop to the maximum size you will need.  This avoids frequent memory reallocations which hurt performance and eventually will crash the program.

 

Lynn

 

2D Array.png

0 Kudos
Message 2 of 16
(9,425 Views)

You will need to remove the extra element on the Index Array function for this to work.

 

Edit: This statement was in error, but I posted before I caught it.

-----------------------------------------------------------------------------------------
Reese, (former CLAD, future CLD)

Some people call me the Space Cowboy!
Some call me the gangster of love.
Some people call me MoReese!
...I'm right here baby, right here, right here, right here at home
0 Kudos
Message 3 of 16
(9,414 Views)

I want to record data for an extended period of time, so I will use a while loop. How would I incorporate a while loop into your code?

0 Kudos
Message 4 of 16
(9,404 Views)

Try this.  You can adjust the timer setting while the measurements are taking place.  The array size that it's initialized to is irrelevent because it will continue to append to the array until the timer expires.

-----------------------------------------------------------------------------------------
Reese, (former CLAD, future CLD)

Some people call me the Space Cowboy!
Some call me the gangster of love.
Some people call me MoReese!
...I'm right here baby, right here, right here, right here at home
0 Kudos
Message 5 of 16
(9,394 Views)

Sorry, the stop button was behind the time delay.

-----------------------------------------------------------------------------------------
Reese, (former CLAD, future CLD)

Some people call me the Space Cowboy!
Some call me the gangster of love.
Some people call me MoReese!
...I'm right here baby, right here, right here, right here at home
0 Kudos
Message 6 of 16
(9,391 Views)

When I change it to a while loop, the output doesn't register (the data only seems to exist inside the while loop and is wiped as soon it exits the while loop).

 

However, in the for loops, the data exists outside the for loop. Does anybody know why it happens this way? If I want to use a while loop, would I have to create a reference to a data field within the while loop?

Download All
0 Kudos
Message 7 of 16
(9,381 Views)

@MoReese:

 

Could you post a screenshot of the VI, or vi compatible with labview 7? (yes i'm behind on the times)

0 Kudos
Message 8 of 16
(9,377 Views)
Solution
Accepted by topic author compstopmer

Here you go.  Change the tunnel on the while loop to a shift register.  This will accumulate the data from one iteration to the next.  This is similar to the .jpg for loop in your post.  I'm not sure, but your output shift register on the for loop doesn't appear to be wired to the Insert to Array function output.

-----------------------------------------------------------------------------------------
Reese, (former CLAD, future CLD)

Some people call me the Space Cowboy!
Some call me the gangster of love.
Some people call me MoReese!
...I'm right here baby, right here, right here, right here at home
0 Kudos
Message 9 of 16
(9,366 Views)

So for loop and timed while loop work, do you know why a normal while loop won't work?

0 Kudos
Message 10 of 16
(9,357 Views)