Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
2 ratings:
 2 out of 5   

Passing 2D Arrays between LabVIEW and CVI or Measurement Studio with Network Variables



Primary Software:
Primary Software Version: N/A
Primary Software Fixed Version: N/A
Secondary Software: Measurement Studio>>Measurement Studio for .NET Support

Problem:
When I pass a 2D array of data from LabVIEW to another programming environment, such as CVI or Visual Studio, the data becomes garbled.  I am using a shared variable or network variable as the communication layer.

For example, I have a 2 x 3 array in LabVIEW that looks like this:
2D Array in LabVIEW
1 2 3
4 5 6

When I read the data in another environment it looks like this:
Garbled 2D Array
1 4 2
5 3 6


Solution:
This misinterpretation occurs because LabVIEW converts arrays to Column-Major Order before writing to network shared variables, and expects arrays to be in this format when reading from network shared variables. However, CVI and Measurement Studio do not do this conversion when writing to and reading from network shared variables, but rather use Row-Major Order. Take the following array as an example:

Example Array
1
2
3
4
5
6


When LabVIEW writes this array to a network shared variable, it writes it as 1, 4, 2, 5, 3, 6. From CVI or Measurement Studio, the same array would be written as 1, 2, 3, 4, 5, 6.

When you pass data between these two different languages, it is passed as a list of numbers.  The order type information is not passed with the array, causing the aforementioned misinterpretation. You can avoid this by writing an algorithm that will change one format into the other.

There are two possibilities for making this conversion. First, you could convert the data being passed from LabVIEW into the network shared variable from Column-Major Order to Row-Major Order.  Using this method, when you read data from the network shared variable in LabVIEW, you would first need to convert it from Row to Column before you could use it.

You can used the attached VIs located at the bottom of this document to do both of these conversions.  See the below screen shots showing how to use these VIs in LabVIEW.  The VI with the icon picture "Convert 2-D" is the ConvertToRow.vi.




Note: To pass a 2D Array with a network shared variable, you must make the network shared variable of type variant. 

The second possibility would be to flatten the multi-dimensional array to a one-dimensional array when it is written to the network shared variable. When reading from the network shared variable, you would do the opposite. This approach should be more efficient, and will work for arrays of greater than two dimensions. There are also VI's for flattening a multi-dimensional array to a one dimensional array and vice versa. These are attached below.

Related Links:
Wikipedia: Row-Major Order

Attachments:
ConvertToRow.vi
ConverterFromRow.vi

BuildArray.viFlattenArray.vi


Report Date: 01/31/2008
Last Updated: 11/30/2011
Document ID: 4HUH7LQX

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit