LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

VI of the Day (9/15/2009) - Insert Into Array

Here is another function that made my day when it was introduced (or at least the day I discovered it), "Insert Into Array".  Imagine yourself before this function and you need to insert a new column into your 2D array.   Here comes some for-looping, indexing, array building, memory thrashing madness.  Sometimes you could be clever and throw in some rotation as well.  Now things have gotten a lot better.  (All those things probably still go on, but if built-in functions do it, its ok and conveniently swept under a little yellow rug).

 

Now we just do this to insert a column:

 

InsertIntoArray.png

 

Groundrules for VIOTD here

Message 1 of 7
(3,529 Views)

Very handy indeed, but preallocate your array and keep track of its size when using Insert Into Array. Consider Replace Array Subset before using Insert Into Array. My 2c.

 

Richard






Message 2 of 7
(3,469 Views)

I think it is important to say that while it is very useful for certain activities with arrays, there are a lot of time it is misused.  Many new users try to use it when they should be using the Build Array function.  When putting a new element at the end of 1-D array, or row at the end of a 2-D array (or even the beginning), build array is usually better to use and easier to use.  Doing it with Insert into Array needs more care and a minor programming mistake leads to some one trying to insert something into the middle of an empty part of an array and questioning why they aren't getting any results.

 

Overall, I'd say I use Build Array 90-95% of the time and Insert into array, much more rarely, perhaps 5%.

Message 3 of 7
(3,455 Views)

Ravens,

 

But neither Insert' nor Build' can be done in-place, right?  

Richard






0 Kudos
Message 4 of 7
(3,439 Views)
I think you're correct.  But someone with more experience with the relatively new InPlace structure should confirm.
0 Kudos
Message 5 of 7
(3,420 Views)

You should note that doing something in place is not limited to the IPE - LabVIEW has been doing it for years and still does. It simply means doing some work without needing to allocate a new buffer.

 

Anyway, you can probably do it in place for both of them if you insert an empty array. 😉

 

By the way, I haven't checked, but I'm assuming the LV compiler will initially try to reuse the existing memory location by enlarging it to fit the new size of the array, opting to actually move the array in memory only if there is no room for the resized array. That said, it's only a guess of an implentation detail.


___________________
Try to take over the world!
0 Kudos
Message 6 of 7
(3,411 Views)

Ravens Fan's post did jog my memory of something I wanted to point out.  I tend to use the function to "fix" data files.  For example, I was logging several complex values with columns for real and imaginary parts.  Later I decided it would be useful to add magnitude and phase as well.  I first changed the data taking VIs to make the change and then fixed the old data to match using this function.

 

My synopsis, much like Ravens Fan said, it is great to have it around when you need it, but if you are using it during the initial data creation I think you should reconsider.

 

Look for the IPE to be appearing as a VIOTD soon, I just need to figure out something more intelligent to say about it than "Where have you been all my life?"

 

 

0 Kudos
Message 7 of 7
(3,394 Views)