Showing results for 
Search instead for 
Did you mean: 

Dynamically Build/Update 3D Array

Go to solution

Hello to all,


I am having an issue with dynamically building a 3D array from the following dataset:


x: {1,2,3,4,5}

y: {1,2,3,4,5}


Z_1: {1,2,3,4,5}

Z_2: {1,2,3,4,5}

Z_3: {1,2,3,4,5}

Z_4: {1,2,3,4,5}

Z_5: {1,2,3,4,5}


where there are (5) Z amplitudes associated with each measurement location.  What I am able to do statically (passing in the above values of x and y 1D arrays and Z 2D array) is to create a 3D array consisting of x and y indexed intensity graph-formatted 2D arrays.  Each page of the array corresponds to each of the 5 Z amplitudes measured at each spot.  In the attached VI, the Build Array function works perfectly for the static case where the full data set is available.  I followed an example from another forum discussion that stipulated to nest two For loops building the 2D array in the inner loop and incrementing the pages on the outer loop. The static situation is solved.


However, my goal is to graphically present the data as it is taken, point by point, to the user.  The other attached VI simulates this scenario, incrementing the x and y positions based on the Measure button being depressed.  The z Matrix indicator shows that the 2D Array (each individual page) is in the proper format.  The building of the 3D array dynamically is where I am having problems.  I have tried Build Array and Replace Array Subset, but neither of these functions produce the intended result.


In the Build Array case, the 3D array is built correctly on the first measurement: for x=y=1, page0 2D array has the Z_1 value in position (1,1), page1 has the Z_2 value in position (1,1), page2 Z_3, etc.  However, on the second measurement, as expected, the arrays are concatenated so that page5 has the Z_1 values in both the (1,1) and (2,2) positions.  This result is correctly formatted, however, the 3D array page counted should be restricted to 5 (page0,1,2,3,4).  So ideally, page5 in this case should replace page0.  So Build Array results in the correct format but the concatenation is undesirable.


The Replace Array Subset was implemented with the page number index (outer for loop) used to specify the 3D array subset to be replaced.  The result was an empty 3D array.


Thank you any assistance in resolving this issue.




Download All
0 Kudos
Message 1 of 14

Please don't post oversized front panels. Not eveybody has 4kx4k monitors.


Just glancing at the 3D array test vi, this thing is buggier than a formicarium.


Reshaping an empty array remains an empty array, so why all that song and dance with array max, etc.?


You probably know the final array size, so all you need to do is initialize a 3D array of NaN, then replace elements as you go with real data. Place the indicator and a delay in the innermost loop if you want to watch the data build in real time. 

0 Kudos
Message 2 of 14

Learn about auto-indexing.


I see several places in your code where you check the array size and wire that to N.  Then feed the array into the For Loop and use Index Array with the iteration node wired to it to get an element.


If you made the tunnel an auto-indexing tunnel, you could get rid of all that crap.


Also, in one case you use a For Loop to make a simple 5 element array from 1 to 5.  Use the Ramp function to build that array.  Or, since the array is so small, just make an array constant with those 5 elements set in that array.

0 Kudos
Message 3 of 14

In additions, it is completely impossible to verify correct operations if the array looks basically the same in all directrons and there are duplicate rowns, columns, and pillars. How can you ever tell which "3" ends up where??? This is no way to design and test a program.


For real troubleshooting, you need to ensure that every single element in the 3D array is unique!


In any case, as has been mentioned, you code is incredibly convoluted. You know the array size from the beginning, so initialize a 3D array filled with NaN and keep it in a shift register. As new data arrives (one elemens or one slice at a time, whetever), place it in the correct location using "replace array subset".


Attached are three example VIs, modified from your "3d array test". While the code is singificantly simpler, it actually does much more. I simply removed all the dead wood, duplicate code and unecessary constructs.


The first one just builds the 3D array and slices out a plane according to the ring selection, plane selection, and cursor position, as appropriate.  (see image)

The other two examples use the same data array, but incrementally fills an initialized NaN 3D array with the data, either one plane at a time or one element at a time, starting over with a NaN array once everything is filled. It should hopefully give you enough tools and hints to complete your code with the oversized black front panel. I' am not going to touch that! (cannot work on it on a small laptop screen!).

You probably need to make changes depending on which dimension you call x or y. Should be trivial after some trial and error.


Of course the incremental examples currently use the preformed data. It would be equally simple to generate each new plane or element inside the loop to simulate data acquisition. As I said, you canily go back to your original data (once everything is verified!) by removing the multiplications and additions in the three small loops and simply wire the array across, adding another dimension each time (as shown here). Make sure to disable autoindexing and the input tunnel.


Download All
0 Kudos
Message 4 of 14

To the LabVIEW Champions,


Thank you for your insight and suggestions on how I need to improve my coding. Your examples are invaluable to me, altenbach (providing me with sample code and anytime I read your posts in the discussion forum - thanks). I'm just now returning to my computer after being sidelined with illness.


Replace Array Subset, as pointed out, works great if there is an initialized array of known dimensions.  My issue is that I have to build an array dynamically - the array size is unknown at run time.  My populated example arrays were the static case.  The dynamic case when an arbitrary number of measurements are taken, from which a 3D array is populated with each measurement, is my real problem (in addition to my lack of clean coding, poor choice of test array items, lack of use of auto-indexing, etc.).


Any suggestions on how to handle populating the 3D array of unknown size?


Thank you



0 Kudos
Message 5 of 14

Hi Kmsk,


Insert into Array would probably be your best option.  It is not necessary to know the array size to add elements to the array using this function.  If you would like to learn more about arrays and array functions in LabVIEW, I recommend this tutorial:


I hope this helps!


Dayna P.

Applications Engineer

National Instruments

0 Kudos
Message 6 of 14
Accepted by kmsk

kmsk wrote: 

Any suggestions on how to handle populating the 3D array of unknown size?

If the final array size has a reasonable upper limit, you can initialize a slightly oversized array, then keep using replace array subset. It will be significanlty faster and more memory efficient than incrementally growing large arrays.

When the acquisition is done, you can trim to the actual final size.


Appending planes to a 3D array using "insert into array" or "Build array" (preferred?), will cause frequent memory reallocations and could slow down your code by orders of magnitude, while casing excessive memory fragmentation.



Message 7 of 14



Thank you for pointing out the memory and execution time considerations for each array initialization and update method.  I remember reading a thread that compared the different methods.


I will be utilizing your elegant example code you sent earlier.


Thank you very much!

0 Kudos
Message 8 of 14

Hi Altanbach


 I am also looking for the same type of solution but my labview is 2009 one. Can you please re-upload the VIs with 2009 version so that It can be very useful for me.


Thanks in Advance


0 Kudos
Message 9 of 14

@Mahendar wrote:

Can you please re-upload the VIs with 2009 version so that It can be very useful for me.

Sure, here are the 2009 versions. Unfortunately, the original controls and indicators were in silver style, which did not exist in 2009, so they look a bit ugly in 2009.

It's the code that matters, so I did not bother to change anything.

0 Kudos
Message 10 of 14