2
votes

I've read the post "Read and write from/to a binary file in Matlab" but I still have doubts. I have a binary file of long double values created with fwrite in C and in Matlab I'm using

fid = fopen('vz3.dat', 'r')
mydata = fread(fid, 'double')

where vz3.dat is my file. But I'm getting garbage values in Matlab. According to

[cinfo, maxsize, ordering] = computer

in Matlab, my computer is a little-endian system (byte ordering system). Any suggestions?

By the way, does a binary file necessarily have to end in .bin .I'm using the .dat extension. Is it ok to do so?

Thanks a lot

2
From my experience, binary files can end in just about anything you want. I use .raw for the some of my data processing. Using .dat or .bin would probably help avoid confusion though. - Doresoom
Thanks. I don't know why subconsciously I went for dat :) - yCalleecharan

2 Answers

3
votes

To open a file with little endian, use

fid = fopen('vz3.dat','r','l');

It doesn't matter what the file is called, by the way.

1
votes

In case you have to use a file handle opened elsewhere, you can also use the machineformat parameter to fread (which is optional).

The documentation is available on the MathWorks site.