-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbfload.m
83 lines (70 loc) · 2.31 KB
/
dbfload.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function [ d, h ] = dbfload(filename, range, format)
% DBFLOAD Loads data from a DataAcquisition-generated file in a range of
% indices.
%
% [d,h] = dbfload(filename, range, format) - Load range=[start,end] of points, 0-based
% [~,h] = dbfload(filename, 'info') - Load header data only
%
% calling dbfload(filename) without specifying a range will return the
% full dataset
% (... this could be a bad idea depending on the file size ...)
%
% 'format' is supposed to be 'double' or 'int16'
%
% DBFLOAD is called by DataAcquisition and used as a file converter.
% That is the recommended usage.
%
% Stephen Fleming
d = [];
h = [];
% open the file and read header
fid = fopen(filename,'r');
if fid == -1
error(['Could not open ' filename]);
end
% get total length of file
fileinfo = dir(filename);
fileSize = fileinfo.bytes;
% now read number of header points, and the header
nh = uint64(fread(fid,1,'*uint32'));
hh = fread(fid,nh,'*uint8');
h = getArrayFromByteStream(hh);
fstart = 4+nh;
bytesize = 2; % int16
% also, how many points and channels?
h.numChan = numel(h.chNames);
% this is the total number of points
h.numTotal = double(fileSize - fstart)/bytesize;
% this is the number per each channel
h.numPts = double(h.numTotal/h.numChan);
% load data, or just return header?
if nargin > 1 && strcmp(range,'info')
fclose(fid);
return;
end
% find the right place to seek, and do so
if nargin<2
range = [0 h.numPts];
end
% go to start
fseek(fid, fstart+range(1)*bytesize*h.numChan, 'bof'); % double needs an 8 here
% how many points and array size
npts = range(2) - range(1);
sz = [h.numChan, npts];
% now read
d16 = fread(fid, sz, '*int16')';
% abort if the data file is empty
if isempty(d16)
error('dbfload:fileEmpty','Data file is empty.')
end
% decide how to output data
if nargin > 2 && strcmp(format,'int16')
% keep data as 16-bit integers
d = d16;
else
% scale data from 16-bit integer to a double
d = double(d16)./repmat(h.data_compression_scaling,size(d16,1),1);
end
% all done
fclose(fid);
end