Example Code

Insert Item Into Any Array Index

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview:

This function inserts or replaces an element at any index within an array. If the requested index is valid and already exists within the array, the new element will be inserted or replaced into the array. If the index is larger than the size of the array, the array will be padded, and the new element will be appended at the requested position. This VI may be modified to work with different data types as well as inserting a subarray rather than just a single element.

 

Description:

First, the function compares the requested index position with the array size to determine whether an element already exists at this position or if the array needs to be resized.  The user can select to replace or insert at the specified index if it exists.  If it does not, the code uses the Reshape Array function to increase the size of the array, then places the element at the specified index. 

 

This function allows you to avoid adding additional logic each time you manipulate an array and wish to avoid an array index out of bounds error.  It is also useful for allowing reuse to insert, replace, or grow an array without additional coding necessary.  This function could easily be modified to work with other data types.

 

Requirements:

  • LabVIEW 2012 (or compatible)

  

Steps to Implement or Execute Code

  1. Download the Insert Into Any Array_LV80_CA (or Insert Into Any Array Index 2012 NIVerified) VI
  2. Place the VI file into the directory for your project.
  3. Add the VI files into your project (In Project Explorer window, right-click on My Computer, select Add»File...).
  4. Add the VI as subVI into the VI where you plan to use the functionality.
  5. Wire the necessary information into the function icon, creating controls or constants as needed to manipulate the function's behavior.

 

 

Additional Information or References 

 

Front Panel

front_panel.png

 

VI Snippet

1.png

 

2.png

 

 

10-31-2010: Modified by CA for better inplaceness and simpler code. (reshape array replaces initialize array, two built array nodes and associated math)

 

**This document has been updated to meet the current required format for the NI Code Exchange. For more details visit this discussion threadhttps://decibel.ni.com/content/thread/15639**

Daniel S.
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
Todd S.
NI Employee (retired)
on

Are there any existing shipping examples in the LabVIEW Help that show this functionality?  If so, please include some links.  If not, we need to find out how to point the product to examples like these.  Let me know.

Todd S.
LabVIEW Community Manager
National Instruments
cdhuntsman
Member
Member
on

If it's so darn easy to resize an array, why in the heck is there not an "autogrow" boolean property of arrays built right into LabVIEW?  This would give the developer the choice of allowing the array to grow in demand for an index out of bounds inserts, while preserving the (safer default) character of not inserting into out of bounds locations.

Dirtboy
Member
Member
on

Is there any way to find out how labview implements arrays under the hood? I spent all day trying to simply insert a new element into a 2 dimensional array at location (i,j).

Like this:

array = newElement;

Simply can't do it with "insert element into array" You have to insert an entire new array into row i.

I ended up writing "InsertElementByRow" and "InsertElementByCol" Where I extract an entire row, replace the single element in that row, then put the row back, or, extract an entire column, (as a 1 dimensional array), replace that element, then replace the entire column.

But "replace array subset" works for an individual element. I'd like to understand why that is the case.

crossrulz
Knight of NI Knight of NI
Knight of NI
on

2D arrays much be rectangular, meaning that every row must have the same number of items in it.  So it is obvious then that you can't just add a single element into a specific location unless you add an entire row.



There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5