-
Notifications
You must be signed in to change notification settings - Fork 1
/
readHeader.m
68 lines (59 loc) · 1.78 KB
/
readHeader.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
function header = readHeader(varargin)
% Cell of filenames, filename, or directory?
args = {};
for i=1:nargin
if iscell(varargin{i})
args = [args, varargin{i}];
else
args{end+1} = varargin{i};
end
end
filenames = {};
% Loop over all input args:
for argNum = 1:length(args)
% if directory, add files to list of filenames to import:
if isdir(args{argNum})
r = dir(args{argNum});
for i = 3:length(r)
% Only read .dat filenames:
if ~isempty(regexpi(r(i).name,'\.dat'))
filenames{end+1} = r(i).name;
end
end
% if file, add filename to list of filenames:
elseif ~isempty(dir(args{argNum}))
filenames{end+1} = args{argNum}; %#ok<*AGROW>
end
end
header = {};
for fileNum = 1:length(filenames)
% Import the first 200 lines of the file:
% fileHeader = importdata(filenames{fileNum},'',200); % <- slower
fid = fopen(filenames{fileNum});
C = textscan(fid, '%s', 200, 'delimiter','\n');
fclose(fid);
fileHeader = C{1};
nHead = length(fileHeader);
if nHead == 0
fprintf('File %s not readable.\n',filenames{fileNum})
continue
end
% Determine labels and values:
for headLine = 1:nHead
labelAndValue = regexp(fileHeader{headLine}, ': ', 'split');
tempHeadLabel{headLine} = labelAndValue{1};
tempHeadValue{headLine} = [labelAndValue{2:end}];
end
% Add tempHead to header
header{end+1}.label = tempHeadLabel;
header{end}.value = tempHeadValue;
end % for fileNum
if nargin == 1 && ~iscell(varargin{1})
if isempty(header)
warning('MATLAB:readHeader:emptyHeader',...
'Empty header file. Check filename.')
else
header = header{1};
end
end
end