Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI
1 rating:
 1 out of 5     Rate this Document

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

Primary Software: LabVIEW Development Systems>>Base Package
Primary Software Version: 8.5.1
Primary Software Fixed Version: N/A
Secondary Software: Measurement Studio>>.NET Support

Problem:
When I pass a 2D array of data from LabVIEW to another programing 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 mis-interpretation occurs because LabVIEW and other programs internally represent 2D array differently.  LabVIEW along with a few other program use the Column-Major Order to represent 2D arrays, while C and .NET programs represent 2D arrays in Row-Major Order.  Basically, LabVIEW stores the following array

Example Array
1
2
3
4
5
6


in memory as 1, 4, 2, 5, 3, 6. 


This same Example Array is stored in C or a .NET language as 1, 2, 3, 4, 5, 6.

When you pass data between these two different languages, it is passed in a flattened form (the list of numbers).  The order type information is not passed with the array, and thus the mis-interpretation.

You can fix this by writing an algorithm that will change one format into the other.  Just pick which order you want to standardize to and convert.  For example, if you wanted to standardize on the Row-Major Order, you would need to convert data being passed from LabVIEW into the shared variable from Column-Major Order to Row-Major Order.  When you read data from the 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 at the bottom to do both of these conversions.  See the below screen shot of 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 network variable, you must make the network variable of type variant. 

Related Links:
Wikipedia: Row-Major Order

Attachments:
ConvertToRow.vi
ConverterFromRow.vi




Report Date: 01/31/2008
Last Updated: 05/23/2008
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