필터 지우기
필터 지우기

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