读取.dat文件的代码给出索引错误

时间:2018-09-22 13:06:26

标签: matlab file raw-data

我有一个应该读取.dat文件的源代码。代码在Matlab中。每次尝试读取.dat文件时都会遇到错误:

这是.dat文件:https://drive.google.com/file/d/1iQM4P__FQqLCvHHdvkLwbo-jhHNNRXAl/view

  

使用零错误

     

大小输入必须为整数。

     

readBasebandFile错误(第49行)

     

hdrMat =零(numFrames,NumHdrs);

function [ hdrMat, FrameMat ] = readBasebandFile( file )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here

NumHdrs = 6;

fid = fopen(file, 'rb');
if fid < 3
disp(['couldnt read file ' file]);
return
end

f = dir(file);
fsize = f.bytes;

% read first frame
ctr = 0;
hdrMat = [];
FrameMat = [];

while (1)
if feof(fid)
    break
end

% read header
%frame counter
frameCtr = fread(fid, 1,'uint32');
numBins = fread(fid, 1,'uint32');
binLength = fread(fid, 1,'single');
% sampling frequency which defines the range resolution through
% binLength = C/Fs/2, where C is the speed of light in the medium. 
Fs = fread(fid, 1,'single');
% carrier frequency
Fc = fread(fid, 1,'single');
RangeOffset = fread(fid, 1, 'single');

% check valid header read
if isempty(frameCtr) || isempty(numBins) || isempty(binLength) || isempty(Fs) ... 
        || isempty(Fc) || isempty(RangeOffset)
    break;
end

% read data
data = fread(fid, 2*numBins, 'single');
if ctr==0
    % 2 because it's complex values and 4 because 'single' is 4 bytes. 
   numFrames = fsize / (4*(NumHdrs + 2*numBins)); 
   hdrMat = zeros(numFrames, NumHdrs);
   FrameMat = zeros(2*numBins, numFrames);
end
ctr = ctr + 1;

hdrMat(ctr,:) = [double(frameCtr) double(numBins) binLength Fs Fc 
RangeOffset];
FrameMat(:,ctr) = data;

end

[n,m] = size(hdrMat);
disp([file ' read. NumFrames=' num2str(n)]);
fclose(fid);

1 个答案:

答案 0 :(得分:0)

更改

numFrames = fsize / (4*(NumHdrs + 2*numBins));

使用

numFrames = round(fsize / (4*(NumHdrs + 2*numBins)));

根据需要,也可以使用ceilfloor代替round

如错误消息所示,初始化数组时,您的大小输入必须为整数。例如,您可以有一个2x2矩阵,但没有2.1x2.3矩阵这样的东西。数学上不合逻辑。