-
Notifications
You must be signed in to change notification settings - Fork 11
/
readbvconf.m
152 lines (135 loc) · 5.59 KB
/
readbvconf.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
% readbvconf() - read Brain Vision Data Exchange format configuration
% file
%
% Usage:
% >> CONF = readbvconf(pathname, filename);
%
% Inputs:
% pathname - path to file
% filename - filename
%
% Outputs:
% CONF - structure configuration
%
% Author: Andreas Widmann, University of Leipzig, 2007
% Copyright (C) 2007 Andreas Widmann, University of Leipzig, widmann@uni-leipzig.de
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
% $Id: readbvconf.m 44 2009-11-12 02:00:56Z arnodelorme $
function CONF = readbvconf(pathname, filename)
if nargin < 2
error('Not enough input arguments');
end
%
% % Open and read file (old method, much slower)
% [IN, message] = fopen(fullfile(pathname, filename), 'r');
% if IN == -1
% [IN, message] = fopen(fullfile(pathname, lower(filename)));
% if IN == -1
% error(message)
% end;
% end
% raw={};
% while ~feof(IN)
% raw = [raw; {fgetl(IN)}];
% end
% fclose(IN);
%
% % Remove comments and empty lines
% raw(cellfun('isempty', raw) == true) = [];
% raw(strmatch(';', raw)) = [];
% Open and read file (automatically remove empty lines)
fid = fopen(fullfile(pathname, filename), 'r');
corrupted_string = '--corrupted--';
if strcmpi(strtrim(fileread(fullfile(pathname, filename))), corrupted_string)
fprintf(2, 'Cannot import marker file -- corrupted\n');
return;
end
raw = textscan(fid, '%s', 'delimiter', '');
fclose(fid);
raw = raw{1};
raw(strmatch(';', raw)) = []; % remove comments
% Find sections
sectionArray = [strmatch('[', raw)' length(raw) + 1];
for iSection = 1:length(sectionArray) - 1
% Convert section name
tmpstr = deblank(raw{sectionArray(iSection)});
fieldName = lower(tmpstr(2:end-1));
%fieldName = lower(char(strread(tmpstr(2:end), '[%s', 'delimiter', ']')));
fieldName(isspace(fieldName) == true) = [];
% Fill structure with parameter value pairs
switch fieldName
case {'commoninfos' 'binaryinfos' 'asciiinfos'}
for line = sectionArray(iSection) + 1:sectionArray(iSection + 1) - 1
splitArray = strfind(raw{line}, '=');
fieldName2 = lower(raw{line}(1:splitArray(1) - 1));
fieldName2(fieldName2 == ' ') = '_';
fieldName2(fieldName2 == ':') = '_';
CONF.(fieldName).(fieldName2) = raw{line}(splitArray(1) + 1:end);
end
case {'channelinfos' 'coordinates'}
for line = sectionArray(iSection) + 1:sectionArray(iSection + 1) - 1
splitArray = strfind(raw{line}, '=');
CONF.(fieldName)(str2double(raw{line}(3:splitArray(1) - 1))) = {raw{line}(splitArray(1) + 1:end)};
end
case {'markerinfos'} % Allow discontinuity for markers (but not channelinfos and coordinates!)
% try reading the whole section at once
try
fileData = raw(sectionArray(iSection) + 1:sectionArray(iSection + 1) - 1);
fileData = sprintf('%s\n', fileData{:});
newData = textscan(fileData', '%s', 'delimiter', '=');
newData = newData{1};
eventValues = newData(2:2:end);
eventMarkers = newData(1:2:end-1);
eventMarkers = cellfun(@(x)str2double(x(3:end)), eventMarkers, 'uniformoutput', false);
markerInfo = eventValues;
markerInfo(:,2) = eventMarkers;
CONF.( [ fieldName ] ) = markerInfo;
catch
% old method (much slower)
for line = sectionArray(iSection) + 1:sectionArray(iSection + 1) - 1
splitArray = strfind(raw{line}, '=');
CONF.(fieldName)(line - sectionArray(iSection), :) = {raw{line}(splitArray(1) + 1:end) str2double(raw{line}(3:splitArray(1) - 1))};
end
end
if ~isfield(CONF, fieldName)
disp('No event found');
else
if any(cellfun(@isempty, CONF.(fieldName)(:,1)))
warning('Empty event(s).')
end
if ~all(1:size(CONF.(fieldName), 1) == [CONF.(fieldName){:, 2}])
warning('Marker number discontinuity.')
end
end
case 'comment'
CONF.(fieldName) = raw(sectionArray(iSection) + 1:sectionArray(iSection + 1) - 1);
otherwise
fprintf('Unrecognized entry: %s\n', fieldName);
end
end
% Handle ahdr file type exceptions
[ ~, ~, ext ] = fileparts( fullfile(pathname, filename) );
if strcmp( ext, '.ahdr' )
if ~isfield( CONF.commoninfos, 'numberofchannels' )
error( 'Common infos field numberofchannels required.' )
else
ahdrChan = str2double( CONF.commoninfos.numberofchannels ) + 1;
CONF.commoninfos.numberofchannels = num2str( ahdrChan );
end
CONF.channelinfos(ahdrChan) = { 'test,,1,mV' };
CONF.coordinates(ahdrChan) = { '0,0,0' };
end
disp('Done.')