importing C written binary files into Matlab
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开发者_C百科 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
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.
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.
精彩评论