[422372]: / functions / sigprocfunc / readneurolocs.m

Download this file

202 lines (185 with data), 6.9 kB

  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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
% readneurolocs() - read neuroscan polar location file (.asc)
%
% Usage:
% >> CHANLOCS = readneurolocs( filename );
% >> CHANLOCS = readneurolocs( filename, method, 'key1', val1, ...);
%
% Inputs:
% filename - file name or matlab cell array { names x_coord y_coord }
% method - [1 2, 3, 4 or 5] different import methods
%
% Optional inputs:
% same as caliblocs()
% note that if no optional input are provided, re-centering will be
% performed automatically and re-scaling of coordinates will be
% performed for '.asc' files (not '.dat' files).
%
% Outputs:
% CHANLOCS - EEGLAB channel location data structure. See
% help readlocs()
%
% Author: Arnaud Delorme, CNL / Salk Institute, 4 March 2003
%
% See also: readlocs()
% Copyright (C) 2003 Arnaud Delorme, Salk Institute, arno@salk.edu
%
% 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
function chanlocs = readneurolocs( filename, method, varargin)
if nargin < 1
help readneurolocs;
return;
end
if nargin < 2
plottag = 0;
end
if nargin < 2
disp('There are 5 methods to import Neuroscan files, if one does not work, try others')
try
chanlocs = readneurolocs( filename, 1);
return
catch
disp('Import method 1 failed, trying method 2');
try
chanlocs = readneurolocs( filename, 2);
return
catch
disp('Import method 2 failed, trying method 3');
try
chanlocs = readneurolocs( filename, 3);
return
catch
disp('Import method 3 failed, trying method 4');
try
chanlocs = readneurolocs( filename, 4);
return
catch
disp('Import method 3 failed, trying method 5');
try
chanlocs = readneurolocs( filename, 5);
return
catch
error('No import method worked');
end
end
end
end
end
end
% try new method
if method == 1
locs = readtable(filename, 'filetype', 'text', 'Delimiter', { ';' ' ' '\t' }, 'ConsecutiveDelimitersRule', 'join');
locs = table2cell(locs);
if mod(size(locs,1),2) == 1
error('Issue with file format')
end
halfHeight = size(locs,1)/2;
loc1 = locs(1:halfHeight,:);
loc2 = locs(halfHeight+1:end,:);
x = [loc2{:,3}]; x = x-0.5;
y = [loc2{:,4}]; y = y-0.5;
radius = sqrt(x.^2 + y.^2);
theta = atan2d(x, y);
for iChan = 1:halfHeight
chanlocs(iChan).label = loc1{iChan,2};
chanlocs(iChan).theta = theta(iChan);
chanlocs(iChan).radius = radius(iChan);
end
chanlocs = convertlocs( chanlocs, 'topo2all');
return
elseif method == 2
% read location file
% ------------------
if ischar(filename)
locs = loadtxt( filename, 'delim', 9 );
end
if ~ischar(filename) || locs{1,1}(1) == ';' || size(locs,2) < 5
if ~ischar(filename)
names = filename{1};
x = filename{2};
y = filename{3};
else
if locs{1,1}(1) == ';'
% remove trailing control channels
% --------------------------------
while isnumeric( locs{end,1} ) & locs{end,1} ~= 0
locs = locs(1:end-1,:);
end
% find first numerical index
% --------------------------
index = 1;
while ischar( locs{index,1} ) && index < size(locs,1)
index = index + 1;
end
% extract location array
% ----------------------
nchans = size( locs, 1 ) - index +1;
chans = [locs{end-nchans+1:end, 1:5}];
chans = reshape(chans,nchans,5); %% Added this line in order to get x = chans(:,3)
names = locs(end-nchans*2+1: end-nchans, 2);
for index = 1:length(names)
if ~ischar(names{index})
names{index} = int2str(names{index});
end
end
x = chans(:,3);
y = -chans(:,4);
else
[tmp2 tmpind] = sort( [ locs{:,1} ]);
locs = locs(tmpind,:);
y = [ locs{:,end} ];
x = [ locs{:,end-1} ];
x = x/513.1617*44;
y = y/513.1617*44;
names = locs(:,end-2);
end
end
% second solution using angle
% ---------------------------
[phi,theta] = cart2pol(x, y);
phi = phi/pi*180;
% convert to other types of coordinates
% -------------------------------------
labels = names';
labels = cellfun(@num2str, labels, 'UniformOutput', false);
chanlocs = struct('labels', labels', 'sph_theta_besa', mattocell(theta)', 'sph_phi_besa', mattocell(phi)'); %% labels instead of labels(:)
chanlocs = convertlocs( chanlocs, 'sphbesa2all');
for index = 1:length(chanlocs)
chanlocs(index).labels = num2str(chanlocs(index).labels);
end
% re-calibration
% --------------
chanlocs = adjustlocs(chanlocs, 'autoscale', 'on', 'autorotate', 'off', varargin{:});
end
elseif method == 3
chanlocs = readneurodat( filename);
elseif method == 4
% read location file
% ------------------
if ischar(filename)
locs = loadtxt( filename, 'delim', [9 ' ']);
end
if size(locs,2) == 5
% 5 rows, xyz positions
for index = 1:size(locs,1)
locs{index,3} = - locs{index,3};
end
chanlocs = struct('labels', locs(:,1), 'type', locs(:,2), 'X', locs(:,4), 'Y', locs(:,3), 'Z', locs(:,5));
chanlocs = convertlocs( chanlocs, 'cart2all');
elseif size(locs,2) == 4
chanlocs = readlocs(filename, 'filetype', 'custom', 'format', { 'labels' '-Y' 'X' 'Z' });
end
elseif method == 5
chanlocs = readlocs(filename, 'filetype', 'custom', 'format', { 'labels' 'ignore' '-Y' 'X' 'Z' });
end