0

I have a source code that is supposed to read a .dat file. the code is in Matlab. There is an error I am getting every time I try to read a .dat file:

This is the .dat file: https://drive.google.com/file/d/1iQM4P__FQqLCvHHdvkLwbo-jhHNNRXAl/view

Error using zeros

Size inputs must be integers.

Error in readBasebandFile (line 49)

hdrMat = zeros(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);
Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Anwer Ak
  • 33
  • 7
  • The error message is quite informative here. The `zeros()` function requires integer inputs. In your case, `NumHdrs` is set explicitly to 6, so it must be `numFrames` that is not an integer. – RPM Sep 22 '18 at 13:20
  • what should I change ? – Anwer Ak Sep 22 '18 at 13:31

1 Answers1

0

Change

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

with

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

Alternatively use ceil or floor instead of round depending on what you need.

As the error message indicates, when you initialize an array, your size inputs must be integer. For example, you can have a 2x2 matrix but there is no such thing as 2.1x2.3 matrix. Mathematically illogical.

misaligar
  • 315
  • 3
  • 14
  • After I changed the code, I recieved this error. Subscripted assignment dimension mismatch. Error in readBasebandFile (line 55) FrameMat(:,ctr) = data; – Anwer Ak Sep 22 '18 at 14:52
  • Since I don't know what kind of data you are processing I cannot comment on the details. But in principle you have to make sure the row size of `FrameMat` is equal to the size of the `data` vector. Also make sure `data` is a column vector since you are assigning this variable to all rows of `FrameMat` for a given `ctr` column. Read more here: https://www.mathworks.com/matlabcentral/answers/93586-why-do-i-get-the-subscripted-assignment-dimension-mismatch-error-message – misaligar Sep 22 '18 at 15:43
  • I do not know how I can upload the data file to stack overflow for people to see. do you have a hint of how can I see how many rows in the .dat file – Anwer Ak Sep 22 '18 at 16:11
  • Try `length(data)` or `size(data)` – misaligar Sep 22 '18 at 17:43
  • I still could not open the file please find it attached.https://drive.google.com/file/d/1SchIf40XM9BIxsa5sfK9k13bh_6G_a8j/view – Anwer Ak Sep 24 '18 at 18:19
  • what do you mean by (data)? – Anwer Ak Sep 25 '18 at 19:44