LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

read binary file

Solved!
Go to solution

Hello, I would like to write and then read a 2D array in a binary file. I get an error in my VI and I have no clue how to fix it. Can anyone help me?

Also, should the file extension be .bin or .dat.

Thanks,

Zied

0 Kudos
Message 1 of 4
(3,137 Views)

1.  It doesn't matter what you call the file extension.  Every file in the world is technically binary.  Make the file extension whatever you want.  .bin would make sense because on sight, you get an idea of what the data looks like in it.

 

2.  You don't want to read the file size.  You want to divide that be 8.  Since you are dealing with doubles, each element is 8 bytes.

 

3.  You are going to have problems trying to deal with 2-D arrays.  First, you aren't prepending the array size, so the file is nothing but a string of 8-byte doubles.  It doesn't know how many there are, whether they are individual scalars, a 1-D array, or a 2-D array that could have any number of row or columns.

 

You could try Flatten to String and writing that.  Then read the string and try Unflatten String to get it back.  Otherwise you will need to find someway of putting in the fact it is a 2-D array, what size, and how many columns and rows it has.  Then proceed to read that many bytes, then reshape the array to the appropriate number of rows and columns.

 

Check the example finder for how to read and write binary files.  You may also want to look at the TCP/IP examples.  Though not what you are doing, they are a good example of how to prepend useful information to the beginning of a data stream, then reading and decoding it later to determine how to work with the rest of the data.

Message 2 of 4
(3,119 Views)
Solution
Accepted by topic author ziedhosni

You really do need to prepend the array size.  Otherwise you will have no clue what your two dimension sizes should be.  Then just do a simple read with a 2D array as your data type.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 3 of 4
(3,111 Views)

The key thing to do what Crossrulz shows is that you need to completely remove the "Count" input to the read.  Allow it to be assumed to be one.  Then the Read will connect okay to the 2-D array.  Otherwise you'll get a broken wire because the output will be a 1-D array of clusters, and the clusters are the 2-D arrays.

Message 4 of 4
(3,106 Views)