From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Save LabView data in .mat file with MatFileVI

Author(s): Giovanni D'Avanzo
NI Product(s) Used: NI LabView 2016 (b4bit)


Save data in a .mat file in LabView

 

 

Create a binary file with the layout of the .mat files

 

Introduction

We are often in a position to use data processing with alternative software such as matlab. The need to use .mat files becomes essential in these cases. For this reason a library has been created (in expansion) that allows to save data from labview directly in .mat format to be easily read from the matlab environment without further steps.

 

Application Description

Any file is always made up of bits. The difference in formats is only in the way in which the bit strings are interpetrated. LabView allows you to save data in binary files so you can make any file format from the LabView environment as long as you know its exact shape. On the web there are several jobs made available to users explaining the exact shape of the .mat files, for example to this address.

 

fig.1 MatFile Format Structurefig.1 MatFile Format StructureThe basic structure of the .mat file format is shown in Figure 1.
The structure is essentially made up of two basic blocks:
  - the first is the MAT-file header that describes the .mat file
  - the second is the format that characterizes the type of data whether it is a double or complex array, strings or a single data of any type recognized and used  by the matlab and labview environment.

fig.2 Simple Data Formatfig.2 Simple Data FormatWhile the first part is the same and common to each type of data, the second is characteristic of the specific data, consequently it takes on a different format and description. However, basic are all constituted by three blocks. The type of data, the size it needs and the data itself like fig.2.
The data type are identified by an integer on 32 bits that are tabulated.

                                                                                                                                                                          

 

 

 

 

A further example is illustrated in Figure 3 which is the schematifig.3 Matrix Layoutfig.3 Matrix Layoutc representation of the structure in which the 2D arrays are saved. In this example, you notice an important feature that is padding for bytes when useful information in tags or data does not cover an integer multiple of 64 bits.

To view the bits that make up a file of any kind you can make use of the HxD program.

 

 

 

 

 

 

At this point we just have to build our binary file in .mat format through the use of the LabView File I/O library.
The first part to be constructed is the MAT-header section consisting of 128 bytes.
The first 116 bytes are descriptive, indicate the version of the .mat file the platform on which it was built and the date and time of creation. Other information can be added, but if the 116 bytes are not filled they must be completed with space. After you have to build two blocks of 4 bytes indicate the offset of the subsystems (leave them empty) and 4 bytes indicate the saving mode of each block, by default we will use Little Endian which is encoded on 16 bits as an integer equal to 19785 miINT16.

The following is the LabView code to build the MAT-file header.

fig.4 MAT-file header.fig.4 MAT-file header.

In this example the first string of the file is constant and with each new creation the string of the date and time of creation is added. Count the used bytes remain 48 bytes to complete in which spaces are added. At this point we need to add the offset points that we set to empty so they are 2x4bytes all to zero, to do this we use a 64bit integer (I64).
The version field must be set as 0x0100 which corresponds to a 16-bit integer (I16) equal to 256. After that the endian indicator field must be set with the integer number (I16) equal to 19785 which stands for little endian.

The additional section indicates the type of data to be saved in the format the size and other information such as the name in which the array must be saved.

We will follow this example, adapting it to the case of arrays of real numbers 1xN.

fig.5 Array Layoutfig.5 Array Layout
 
We have to build the tag that is made up of 8 bytes divided into two parts. The first 4 identify the type that must be miMATRIX indicated with an I32 equal to 14, and the total size occupied by the information and data.
This dimension must be calculated by following this simple equation. Considering that there are 3 sections with 16 bytes + 8 bytes that identify the type and the space occupied by data only + 8 bytes for the product between the number of rows and columns.
artLabview7.PNG

 

fig.6 Flag Layoutfig.6 Flag Layout

In the example case we will consider a one-dimensional array for which we need dim = 48 + 8 + N * 8.
At this point we can build the flag array, consisting of a 32bit unsigned integer corresponding to the miUINT32 code, which is the number 6, followed by the integer with a 32bit value of 8 that defines the size of the second part of the flug.
the second part of the flug consists of a zone of 16bit undefined followed by a sequence of 8 bits of which the first four are not defined 3 bits identify the complex type global and logical followed by 8 bits that identify the class ,the class type defines whether it is a double array rather than an array of integers and a cell type structure or array, and 32 bits not defined according to the following by figure 6. Since by default we use the little endian the first four bytes of the flug must be saved in the binary file in reverse order. Then we will first build the class then the 8 bits of the flug and then the 16 undefined bits followed by the remaining 32 bits not defined.

fig.7 LabView Code Flug Arrayfig.7 LabView Code Flug Array

 


 

 We can move on to the description of the dimensions consisting of 16 bytes of which the first 8 define the type and size, we have a 32-bit integer that defines the type miINT32 with code 5 and 32 bits that define the size of 8 bytes. The dimensions are defined in this case by two 32-bit blocks that define the number of rows and columns, in our case 1 and N. 

Let's move on to the definition of the name of the array consisting of 16 bytes of which the first 8 define the type and the size occupied, it is always myINT8 coded with 1 on 32 bits and a block of 32 bits that includes the number 8. In the following 8 bytes we write the name of the array we want to be displayed in the matlab workspace.fig. 8 LabView Code Array name and Data savefig. 8 LabView Code Array name and Data save

 

At this point we have to define the data field that is structured with 4 bytes that define the type 4 bytes that define the occupied dimension and the data sequence.

You can use the HxD program to view the final result. You will get the following result.

fig.9 MatFile Completefig.9 MatFile Complete

 

 

 

Conclusion

Saving .mat files directly from LabView can be very convenient when data processing at the time of acquisition is risky. Even in cases where data storage for processing is required by users who do not take measurements or do not have LabView equipment or knowledge. This approach allows you to share data in a format that can be recognized by MatLab and be processed.

For VIs it is possible to contact the author by email or via LinkedIN, i will send you a copy of the subVI and a sample code of use.

email: davanzo.giovanni.91@gmail.com



badgeMail.PNG

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

Contributors