Clear Filters
Clear Filters

reading binary files

4 views (last 30 days)
Chris
Chris on 28 Jan 2011
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

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jan 2011
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] .';

More Answers (0)

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!