📄 readlocs.m
字号:
'besa' ... 'xyz' ... 'sfp' ... 'loc' ... 'sph' ... 'asc' ... 'dat' ... 'elc' ... 'chanedit' ... 'custom' }; listimportformat = { ... { } ... % polhemus (specific non-columnar implementation) { } ... % polhemus (specific non-columnar implementation) { } ... % polhemus (specific non-columnar implementation) { 'labels' 'sph_theta_besa' 'sph_phi_besa' 'sph_radius' } ... % BESA/EGI format { 'channum' 'X' 'Y' 'Z' 'labels' } ... % xyz format { 'labels' '-Y' 'X' 'Z' } ... % sfp format { 'channum' 'theta' 'radius' 'labels' } ... % loc format { 'channum' 'sph_theta' 'sph_phi' 'labels' } ... % sph format { } ... % ascii Neuroscan format { } ... % ascii Neuroscan format { } ... % eetrak format { 'channum' 'labels' 'theta' 'radius' 'X' 'Y' 'Z' 'sph_theta' 'sph_phi' 'sph_radius' } }; %chanedit formatlistcolformat = { 'labels' 'channum' 'theta' 'radius' 'sph_theta' 'sph_phi' ... 'sph_radius' 'sph_theta_besa' 'sph_phi_besa' 'gain' 'calib' 'type' ... 'X' 'Y' 'Z' '-X' '-Y' '-Z' 'custom1' 'custom2' 'custom3' 'custom4' 'ignore' 'not def' };listskipline = [ ... 0 ... % polhemus, not applicable 0 ... % polhemus, not applicable 0 ... % polhemus, not applicable -1 ... % besa 0 ... 0 ... 0 ... 0 ... 0 ... 0 ... 0 ... 1 ]; % skip the 2 lines header for the chanedit format% ------------------------------------------------------% special mode for getting the infos% ----------------------------------if isstr(filename) & strcmp(filename, 'getinfos') eloc = listtype; labels = listimportformat; theta = listcolformat; radius = listskipline; return;elseif isstr(filename) & strcmp(filename, 'getinfoswrite') eloc = listtype([4:8,10]); labels = listimportformat([4:8,10]); theta = listcolformat([4:8,10]); radius = listskipline([4:8,10]); return;end;g = finputcheck( varargin, ... { 'filetype' 'string' {} ''; 'skiplines' 'integer' [0 Inf] []; 'elecind' 'integer' [1 Inf] []; 'format' 'cell' [] {} }, 'readlocs');if isstr(g), error(g); end; if isstr(filename) % format auto detection % -------------------- if strcmpi(g.filetype, 'autodetect'), g.filetype = ''; end; g.filetype = strtok(g.filetype); periods = find(filename == '.'); fileextension = filename(periods(end)+1:end); g.filetype = lower(g.filetype); if isempty(g.filetype) switch lower(fileextension), case {'loc' 'locs' }, g.filetype = 'loc'; case 'xyz', g.filetype = 'xyz'; fprintf( [ 'WARNING: Matlab Cartesian coord. file extension (".xyz") detected.\n' ... 'If importing EGI Cartesian coords, force type "sfp" instead.\n'] ); case 'sph', g.filetype = 'sph'; case 'ced', g.filetype = 'chanedit'; case 'elp', g.filetype = 'polhemus'; disp( [ 'WARNING: Polhemus Cartesian coord. file extension (".elp") detected.\n' ... ' If importing BESA spherical coords, force type "besa" instead'] ); case 'asc', g.filetype = 'asc'; case 'dat', g.filetype = 'dat'; case 'elc', g.filetype = 'elc'; case 'eps', g.filetype = 'besa'; case 'sfp', g.filetype = 'sfp'; otherwise, g.filetype = ''; end; fprintf('readlocs(): ''%s'' format assumed from file extension\n', g.filetype); else if strcmpi(g.filetype, 'locs'), g.filetype = 'loc'; end; end; % assign format from filetype % --------------------------- if ~isempty(g.filetype) & ~strcmpi(g.filetype, 'custom') ... & ~strcmpi(g.filetype, 'asc') & ~strcmpi(g.filetype, 'elc') & ~strcmpi(g.filetype, 'dat') indexformat = strmatch(lower(g.filetype), lower(listtype), 'exact'); g.format = listimportformat{indexformat}; if isempty(g.skiplines) g.skiplines = listskipline(indexformat); end; if isempty(g.filetype) error( ['readlocs() error: The filetype cannot be detected from the \n' ... ' file extension, and custom format not specified']); end; end; % import file % ----------- if strcmp(g.filetype, 'asc') | strcmp(g.filetype, 'dat') eloc = readneurolocs( filename ); eloc = rmfield(eloc, 'sph_theta'); % for the conversion below eloc = rmfield(eloc, 'sph_theta_besa'); % for the conversion below elseif strcmp(g.filetype, 'elc') eloc = readeetraklocs( filename ); eloc = rmfield(eloc, 'sph_theta'); % for the conversion below eloc = rmfield(eloc, 'sph_theta_besa'); % for the conversion below elseif strcmp(lower(g.filetype(1:end-1)), 'polhemus') | ... strcmp(g.filetype, 'polhemus') try, [eloc labels X Y Z]= readelp( filename ); catch, error('readlocs(): Error reading Polhemus coords. If BESA .elp file, force file type to BESA.'); end; if strcmp(g.filetype, 'polhemusy') tmp = X; X = Y; Y = tmp; end; for index = 1:length( eloc ) eloc(index).X = X(index); eloc(index).Y = Y(index); eloc(index).Z = Z(index); end; else % importing file % -------------- array = load_file_or_array( filename, max(g.skiplines,0)); if size(array,2) < length(g.format) fprintf(['readlocs() warning: Fewer columns in the input than expected.\n' ... ' See >> help readlocs\n']); elseif size(array,2) > length(g.format) fprintf(['readlocs() warning: More columns in the input than expected.\n' ... ' See >> help readlocs\n']); end; % removing lines BESA % ------------------- if g.skiplines == -1 if isempty(array{1,2}) disp('BESA header detected, skipping three lines...'); array = load_file_or_array( filename, -2); end; end; % removing comments and empty lines % --------------------------------- indexbeg = 1; while isempty(array{indexbeg,1}) | ... (isstr(array{indexbeg,1}) & array{indexbeg,1}(1) == '%' ) indexbeg = indexbeg+1; end; array = array(indexbeg:end,:); % converting file % --------------- for indexcol = 1:min(size(array,2), length(g.format)) [str mult] = checkformat(g.format{indexcol}); for indexrow = 1:size( array, 1) if mult ~= 1 eval ( [ 'eloc(indexrow).' str '= -array{indexrow, indexcol};' ]); else eval ( [ 'eloc(indexrow).' str '= array{indexrow, indexcol};' ]); end; end; end; end; % handling BESA coordinates % ------------------------- if isfield(eloc, 'sph_theta_besa') if isnumeric(eloc(1).labels) disp('Alternate BESA format detected ( Theta | Phi )'); for index = 1:length(eloc) eloc(index).sph_phi_besa = eloc(index).sph_theta_besa; eloc(index).sph_theta_besa = eloc(index).labels; end; eloc = rmfield(eloc, 'labels'); elseif isstr(eloc(1).sph_theta_besa) disp('Alternate BESA format detected ( E_type| Elec | Theta | Phi )'); for index = 1:length(eloc) eloc(index).labels = eloc(index).sph_theta_besa; eloc(index).sph_theta_besa = eloc(index).sph_phi_besa; eloc(index).sph_phi_besa = eloc(index).sph_radius; eloc(index).radius = 1; end; end; try eloc = convertlocs(eloc, 'sphbesa2all'); eloc = convertlocs(eloc, 'topo2all'); % problem with some EGI files (not BESA files) catch, disp('Warning: coordinate conversion failed'); end; fprintf('Readlocs: BESA spherical coords. converted, now deleting BESA fields\n'); fprintf(' to avoid confusion (these fields can be exported, though)\n'); eloc = rmfield(eloc, 'sph_phi_besa'); eloc = rmfield(eloc, 'sph_theta_besa'); % converting XYZ coordinates to polar % ----------------------------------- elseif isfield(eloc, 'sph_theta') try eloc = convertlocs(eloc, 'sph2all'); catch, disp('Warning: coordinate conversion failed'); end; elseif isfield(eloc, 'X') try eloc = convertlocs(eloc, 'cart2all'); catch, disp('Warning: coordinate conversion failed'); end; else try eloc = convertlocs(eloc, 'topo2all'); catch, disp('Warning: coordinate conversion failed'); end; end; % inserting labels if no labels % ----------------------------- if ~isfield(eloc, 'labels') fprintf('readlocs(): Inserting electrode labels automatically.\n'); for index = 1:length(eloc) eloc(index).labels = [ 'E' int2str(index) ]; end; else % remove trailing '.' for index = 1:length(eloc) if isstr(eloc(index).labels) tmpdots = find( eloc(index).labels == '.' ); eloc(index).labels(tmpdots) = []; end; end; end; % resorting electrodes if number not-sorted % ----------------------------------------- if isfield(eloc, 'channum') if ~isnumeric(eloc(1).channum) error('Channel numbers must be numeric'); end; allchannum = cell2mat( { eloc.channum } ); if any( sort(allchannum) ~= allchannum ) fprintf('readlocs(): Re-sorting channel numbers based on ''channum'' column indices\n'); [tmp newindices] = sort(allchannum); eloc = eloc(newindices); end; eloc = rmfield(eloc, 'channum'); end;else if isstruct(filename) eloc = filename; else disp('readlocs(): input variable must be a string or a structure'); end; end;if ~isempty(g.elecind) eloc = eloc(g.elecind);end;if nargout > 2 tmptheta = { eloc.theta }; % check which channels have (polar) coordinates set indices = find(~cellfun('isempty', tmptheta)); theta = cell2mat(tmptheta(indices));end;if nargout > 3 tmprad = { eloc.radius }; radius = cell2mat(tmprad(indices));end;%tmpnum = find(~cellfun('isclass', { eloc.labels }, 'char'));%disp('Converting channel labels to string');for index = 1:length(eloc) if ~isstr(eloc(index).labels) eloc(index).labels = int2str(eloc(index).labels); end;end;if nargout > 1 labels = { eloc.labels };end;if isfield(eloc, 'ignore') eloc = rmfield(eloc, 'ignore');end;return;% interpret the variable name% ---------------------------function array = load_file_or_array( varname, skiplines ); if isempty(skiplines), skiplines = 0; end; if exist( varname ) == 2 array = loadtxt(varname,'verbose','off','skipline',skiplines); else % variable in the global workspace % -------------------------- try, array = evalin('base', varname); catch, error('readlocs(): cannot find the named file or variable, check syntax'); end; end; return;% check field format% ------------------function [str, mult] = checkformat(str) mult = 1; if strcmpi(str, 'labels'), str = lower(str); return; end; if strcmpi(str, 'channum'), str = lower(str); return; end; if strcmpi(str, 'theta'), str = lower(str); return; end; if strcmpi(str, 'radius'), str = lower(str); return; end; if strcmpi(str, 'ignore'), str = lower(str); return; end; if strcmpi(str, 'sph_theta'), str = lower(str); return; end; if strcmpi(str, 'sph_phi'), str = lower(str); return; end; if strcmpi(str, 'sph_radius'), str = lower(str); return; end; if strcmpi(str, 'sph_theta_besa'), str = lower(str); return; end; if strcmpi(str, 'sph_phi_besa'), str = lower(str); return; end; if strcmpi(str, 'gain'), str = lower(str); return; end; if strcmpi(str, 'calib'), str = lower(str); return; end; if strcmpi(str, 'type') , str = lower(str); return; end; if strcmpi(str, 'X'), str = upper(str); return; end; if strcmpi(str, 'Y'), str = upper(str); return; end; if strcmpi(str, 'Z'), str = upper(str); return; end; if strcmpi(str, '-X'), str = upper(str(2:end)); mult = -1; return; end; if strcmpi(str, '-Y'), str = upper(str(2:end)); mult = -1; return; end; if strcmpi(str, '-Z'), str = upper(str(2:end)); mult = -1; return; end; if strcmpi(str, 'custum1'), return; end; if strcmpi(str, 'custum2'), return; end; if strcmpi(str, 'custum3'), return; end; if strcmpi(str, 'custum4'), return; end; error(['readlocs(): undefined field ''' str '''']);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -