-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadBinary.m
46 lines (37 loc) · 997 Bytes
/
readBinary.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function data = readBinary(fileName, format)
% The function reads short binary files.
%
% Input: fileName is the name of the binary file to read.
% format is the data format (i.e., 'int16', 'double', etc).
% Currently supported types are 'int16' and 'double'.
%
% Output: data is the data vector read from fileName.
chunkSize = 1000000;
fid = []; data = [];
d = dir(fileName);
if strcmpi(format, 'int16')
nSampsTotal = d.bytes/2;
elseif strcmpi(format, 'double')
nSampsTotal = d.bytes/8;
end
nChunksTotal = ceil(nSampsTotal/chunkSize);
try
fid = fopen(fileName, 'r');
chunkInd = 1;
while 1
fprintf(1, 'chunk %d/%d\n', chunkInd, nChunksTotal);
dat = fread(fid, [1 chunkSize], ['*' format]);
if ~isempty(dat)
data = [data dat]; %#ok<*AGROW>
else
break
end
chunkInd = chunkInd+1;
end
fclose(fid);
catch me
if ~isempty(fid)
fclose(fid);
end
rethrow(me)
end