From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Loading Labview Binary Data into Matlab

This post explains the Labview binary data format. I couldn't find all of this information in any one place so this ought to help anyone in the future.  I didn't want to add any overhead in Labview so I did all of my conversion in Matlab.

 

The Labview VI "Write to Binary File" writes data to a file in a linear format using Big Endian numbers of the type wired into the "Write to Binary File" VI. The array dimensions are listed before the actual array data. 

fid = fopen('BinaryData.bin','r','ieee-be'); % Open the binary file
Dim1 = fread(fid,4); % Reads the first dimension
Dim2 = fread(fid,4); % Reads the second dimension
Dim3 = ...

Each dimension's length is specified by 4 bytes. Each increment of the first, second, third, and fourth byte represent 2^32, 2^16, 2^8, and 1 respectively. 0 0 2 38 equates to 2*256 + 38 = 550 values for that particular dimension.

As long as you know the number of dimensions and precision of your binary data you can load it.

Data = fread(fid,prod([Dim1 Dim2 Dim3]),'double',0,'ieee-be'); % Load double precision data

If you have appended multiple arrays to the same file in Labview you would repeat this procedure. Load each dimension then load the data, repeat.

Data = fread(fid,prod([Dim1 Dim2 Dim3]),'int8',0,'ieee-be'); % Load int8 precision data or boolean data

I had to create a function for my own purposes so I thought I'd share it with everyone else too.  I uploaded it to the Matlab File Exchange.  The file is named labviewload.m.

 

This was tested on Matlab R2007a and Labview 8.2.

 

 

Message 1 of 10
(10,668 Views)

Thanks. I have the same questions as I tried to load labview binary data into Matlab. 

 

-John

0 Kudos
Message 2 of 10
(8,742 Views)

Write and Read binary .bin files: Write .bin in matlab and read in labview. Write .bin in labview and read in matlab

 

https://decibel.ni.com/content/docs/DOC-39038

 

I wanted to add this link to my document on my findings about working with binary data between matlab and labview.

0 Kudos
Message 3 of 10
(7,873 Views)

Hi 🙂

Happy Holiday season.

 

I have collceted some data with Labview and I want to open it in MATALB. Because of the special format of my binary file (a 1D array of a cluster of 2 elements), I've been having difficulty reading it in MATLAB.

 

I have attached a screen shot of my data acquisition process, the collected data (try1) and the VI file that reads the file in Labview. Do you have any suggestions on how to open this in MATLAB?

 

Please let me know if any more info was needed 🙂

 

Thanks

Ashkan

0 Kudos
Message 4 of 10
(7,540 Views)

The original Post is more than 5 years old, and is not completely accurate.  Here are three Read statements -- the first is the Default (where the left and top terminals are unwired), but the other two are also legitimate ...

 

Binary Writes.png

 

The important thing to consider when doing Binary File I/O is that the Reader must know the format used to write the data.  There is nothing that requires a LabVIEW User to use the Defaults (in particular, I write packed sample data without the prepended Array Size, which means I need to read them back knowing the size(s) of the Array).

 

As the Original Poster noted, besides needing to know if the file was written with the default settings for Endian order and whether Size information is present for Arrays and Strings, the Reader also needs to know the exact Type of the Data.  LabVIEW data can range from 8-bit to 64-bit integers (both Signed and Unsigned), from 16 to 64-bit Floats, a 128-bit TimeStamp, and Clusters (similar to a Structure in Matlab, along with a few other types like Complex and Variant).  If you do know all of this, it isn't difficult to write Matlab code to interpret the (now-known) structure of the data and read it into Matlab.

 

Indeed, back when I was doing a lot of Matlab programming, I occasionally would get a data tape (tape?  just how ancient is this guy?) with records whose format was, shall we kindly say, "inadequately documented".  I would read a (tape) record, open it with a Text Editor, and "deduce the Type" by looking for similarities, patterns, and the occasional Ascii string that said "Hi, I'm an Identifier" to me.  I'd then write a trial Matlab routine that implemented my "best guess" as to the data structure and plot the data arrays to see if it "made sense" ...

 

Bob Schor

0 Kudos
Message 5 of 10
(7,517 Views)

(First of all, you should not be appending to an array in an (empty) iniialized shift register. Autoidenxing at the output tunnel would be equivalent and significantly cleaner.)

 

Yes, matlab is little endian by default, so writing in little endian is a reasonable choice.

 

Your data structure is a poor choice. It is rather complicated and will have a lot of "decoratations" (i.e. size headers). You will have an initilal I32 giving the size of the cluster array, but then you also have inner size headers, one for each array cluster element. Since all these inner arrays can potentially have different sizes, there will be a lot of parsing required. I would write a few simple files , then verify using a hex editor (or just read it as string and diplsay in hex format). I don't use matlab enough to know the easiest way to parse these structures.

 

Since you have control over the writing, why can't you pick a more reasonable fomat to begin with? For example, if your inner arrays have a fixed and well known size you could write them as flattenend data, for example.

0 Kudos
Message 6 of 10
(7,509 Views)

@altenbach wrote:

Your data structure is a poor choice. It is rather complicated and will have a lot of "decoratations" (i.e. size headers).


OK, here is a simplified scenario showing the file structure of a similar cluster array (I use I32 inner arrays for simplicity)

 

Let's start reading the hex formatted file data (everything is little endian, of course):

 

0400 0000 : The size of the outer cluster array (4, I32, little endian)

 

0000 0000 : the size of the first array of elment 0 (0, I32, little endian) No element data.

0000 0000 : the size of the second array of elment 0 (0, I32, little endian) No element data.

 

0100 0000:: the size of the first array of element 1 (1, I32, little endian)

0000 0000:  the data of the single element if the first array of element 1.

0100 0000:: the size of the second array of element 1 (1, I32, little endian)

0000 0000:  the data of the single element of the second array of element 1.

 

0200 0000:: the size of the first array of element 2 (2, I32, little endian)

0000 0000 0200 0000:  the data of the two elements of the first array of element 2. [0, 2]

0200 0000:: the size of the second array of element 2 (2, I32, little endian)

0000 0000 0100 0000:  the data of the two element of the second array of element 2. [0,1]

 

0300 0000:: the size of the first array of element 2 (2, I32, little endian)

0000 0000 0200 0000 0400 0000:  the data of the three elements of the first array of element 3. [0, 2, 4]

0300 0000:: the size of the second array of element 3 (3, I32, little endian)

0000 0000 0100 0000 0200 0000:  the data of the two element of the second array of element 2. [0,1,2]

 

end of file.

 

You have DBL arrays, so the array data will be 8 bytes (size header is still 4 bytes I32, of course). As you can see, the file structure is relatively ragged. If you set prepend size=FALSE when writing, the first four bytes would be missing.

 

Message 7 of 10
(7,489 Views)

 

https://decibel.ni.com/content/docs/DOC-39038

 

I wanted to add this link to my document on my findings about working with binary data between matlab and labview.

 

Is it possible to update this link?  When the new NI website came out they blew away all these links.

 

Thanks

Dan Shangraw, P.E.


   

0 Kudos
Message 8 of 10
(4,249 Views)
0 Kudos
Message 9 of 10
(4,241 Views)

Hi Dan,

 

I sent an email to NI Community admins to ask if it was possible to retrieve the information from the link.

If they are unable to retrieve it, I will have to make time to reproduce the example codes.

 

Regards,

 

Manny

0 Kudos
Message 10 of 10
(4,084 Views)