Example Code

LabVIEW - Store Strings between Loop Iterations

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 example contains four VIs to supply four ways to store strings between Loop Iterations.

 

Description

Scenario: 

I am performing an operation which returns string values sporadically - sometimes I have time to read them, although if multiple strings are returned in quick succession I cannot read them.

 

- How can I store these values from one iteration to the next?

 

Solutions:

1. Store all data in a string with EOL constants.

11.png

 

Example using a polling architecture "Basic Store Strings.vi" (or "Basic Store Strings 2012.vi")

1.png

 

In the above example we are using a shift register to store data between iterations.  You can add this to a loop structure by right-clicking the border and selecting "Add Shift Register" - alternatively you can replace standard tunnels with shift registers using the right-click menu. 

If a value of "True" is read from the Add Data control, the True case will execute.  This generates a random number and uses the "Number to Fractional String" conversion to change to string data type.  We then concatenate a carriage return character and the previous string.  For example;

 

Previous String -"Second String

                             First String"

 

Random Number - "0.253615"

 

Resulting String - "0.253615

                              Second String

                              First String"

 

Caveat - This VI uses execution timing of 750 ms to allow the string entry to occur at a rate which is easy to view, however this can cause an unresponsive UI for stopping the VI.

 

Example using an event structure "Basic Store Strings (Events).vi" (or "Basic Store Strings (Events) 2012.vi")

2.png

 

This example solves the issue of timing as the event structure can record asynchronous events.  Otherwise the functionality is identical.

 

2. Store Individual Elements in an Array
22.png

Example using arrays and event structure "Basic Store Strings Array (Events).vi" (or"Basic Store Strings Array (Events) 2012.vi")

3.png

 

Rather than storing this all as a single string, we can create an array of values.  This helps to separate the data.  We can also use this architecture to limit the amount of values stored (for example, the last 10).

 

This is important as when we concatenate strings, or dynamically build arrays. We need to be careful of the amount of memory we are actually using - currently we are resizing the array on every iteration of the loop. This means that the LabVIEW memory allocator has to find a new piece of contiguous memory every loop iteration. This will fragment memory and could lead to out of memory messages.

 

Example using buffering, arrays and event structure "Buffer Store Strings Array (Events).vi" (or "Buffer Store Strings Array (Events) 2012.vi")

4.png

 

This example initialises an array of 5 elements and replaces an element when the "Add Data" button is pressed - we store an index in the numeric shift register and reset this when we reach the highest element.

 

Requirements

  • LabVIEW 2012 (or compatible)

 

Steps to Implement or Execute Code

For the four VIs, the steps to execute are the same

  1. Run the VI.
  2. Click Add Data button and see the data in Stored Data string/array indicator.

 

**This document has been updated to meet the current required format for the NI Code Exchange.** 


Regards,

Peter D

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