reading binary files

4 次查看(过去 30 天)
Chris
Chris 2011-1-28
I am trying to convert a binary file to ascii using the fopen/fread functions. The binary file is in 2 byte integer format with the first column unsigned and the next three columns signed. The format is little endian. Each column is stored with a multiplier of 40, 100, 100, and 100 respectively.
The code I am using is:
clear all; clc;
[FID, message] = fopen('file','rb','l')
precip = fread(FID, [32507,1], 'ushort=>float','l'); tmax = fread(FID, [32507,1], 'short => float','l'); tmin = fread(FID, [32507,1], 'short=>float','l'); wind = fread(FID, [32507,1], 'short=>float','l');
X = [precip/40, tmax/100, tmin/100, wind/100]
The script generates a 4 column matrix where the first 5 rows are:
0 2.96 13.20 24.33;
19.95 3.300 2.32 10.03;
1633.60 17.42 0 1.45;
7.45 3.68 30.16 1.04;
0 2.83 15.49 17.05;
However should be: 0.00 7.98 -1.92 2.98;
0.00 8.47 -2.55 3.32;
0.00 11.88 -0.33 3.20;
0.05 7.78 -1.08 3.03;
0.00 6.86 -4.15 3.00;
Any help with this matter would be greatly appreciated.
Thanks

采纳的回答

Walter Roberson
Walter Roberson 2011-1-28
Binary files do not have any concept of column. Is it possible that the values are interleaved in the file? If so then
bindata = fread(FID, [4,32507], 'ushort=>int16');
X = [double(typecast(bindata(1,:),'uint16')) / 40; ...
double(bindata(2:4,:)) / 100] .';

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by