[422372]: / functions / popfunc / pop_readlocs.m

Download this file

234 lines (212 with data), 9.4 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
% POP_READLOCS - load channel location file (pop up an interactive window
% if no arguments).
%
% Usage:
% >> chanlocs = pop_readlocs; % a window pops up
% >> chanlocs = pop_readlocs( filename, 'key', val, ...); % no window
%
% Inputs:
% filename - Electrode location file name
% 'key',val - Same options as READLOCS (see >> help readlocs)
%
% Outputs: same as READLOCS
%
% Author: Arnaud Delorme, CNL / Salk Institute, 17 Dec 2002
%
% See also: READLOCS
% Copyright (C) 17 Dec 2002 Arnaud Delorme, Salk Institute, arno@salk.edu
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
function [tmplocs, command] = pop_readlocs(filename, varargin);
tmplocs = [];
command = '';
% get infos from readlocs
% -----------------------
[chanformat listcolformat] = readlocs('getinfos');
listtype = { chanformat.type };
if nargin < 1
[filename, filepath] = uigetfile('*.*', 'Importing electrode location file -- pop_readlocs()');
%filename = 'chan32.locs';
%filepath = 'c:\eeglab\eeglab4.0\';
drawnow;
if filename == 0
return;
end
filename = [filepath filename];
tmpfile = loadtxt(filename);
nbcols = cellfun('isempty', tmpfile(end,:));
nbcols = ones(1,length(find(~nbcols)));
% decoding file type
% ------------------
periods = find(filename == '.');
fileextension = filename(periods(end)+1:end);
switch lower(fileextension),
case {'loc' 'locs' }, filetype = 'loc';
case 'xyz', filetype = 'xyz';
case 'sph', filetype = 'sph';
case 'txt', filetype = 'chanedit';
case 'elp', filetype = 'polhemus';
case 'eps', filetype = 'besa';
otherwise, filetype = '';
end
indexfiletype = strmatch(filetype, listtype, 'exact');
% convert format info
% -------------------
formatinfo = { chanformat.importformat };
formatskipcell = { chanformat.skipline };
rmindex = [];
count = 1;
for index = 1:length(formatinfo)
if ~ischar(formatinfo{index})
for index2 = 1:length(formatinfo{index})
indexformat = strmatch(formatinfo{index}{index2}, listcolformat, 'exact');
indexlist(count, index2) = indexformat;
end
if isempty(formatskipcell{index}), formatskip(count) = 0;
else formatskip(count) = formatskipcell{index};
end
count = count+1;
else
rmindex = [ rmindex index ];
end
end
listtype(rmindex) = [];
listtype (end+1) = { 'custom' };
formatskip(end+1) = 0;
indexlist(end+1,:) = -1;
% ask user
formatcom = [ ...
'indexformat = get(findobj(gcf, ''tag'', ''format''), ''value'');' ...
'tmpformat = get(findobj(gcf, ''tag'', ''format''), ''userdata'');' ...
'for tmpindex=1:' int2str(length(nbcols)) ...
' tmpobj = findobj(gcf, ''tag'', [ ''col'' int2str(tmpindex) ]);' ...
' if tmpformat{1}(indexformat,tmpindex) == -1,' ...
' set(tmpobj, ''enable'', ''on'');' ...
' else ' ...
' set(tmpobj, ''enable'', ''off'');' ...
' set(tmpobj, ''value'', tmpformat{1}(indexformat,tmpindex));' ...
' end;' ...
'end;' ...
'strheaderline = fastif(tmpformat{2}(indexformat)<0, ''auto'', int2str(tmpformat{2}(indexformat)));' ...
'set(findobj(gcf, ''tag'', ''headlines''), ''string'', strheaderline);' ...
'eval(get(findobj(gcf, ''tag'', ''headlines''), ''callback''));' ...
'clear tmpindex indexformat tmpformat tmpobj strheaderline;' ];
headercom = [ ...
'tmpheader = str2num(get(findobj(gcf, ''tag'', ''headlines''), ''string''));' ...
'if ~isempty(tmpheader), ' ...
' for tmpindex=1:' int2str(length(nbcols)) ...
' tmpobj = findobj(gcf, ''tag'', [ ''text'' int2str(tmpindex) ]);' ...
' tmpstr = get(tmpobj, ''userdata'');' ...
' set(tmpobj, ''string'', tmpstr(tmpheader+1:min(tmpheader+10, size(tmpstr,1)),:));' ...
' end;' ...
'end;' ...
'clear tmpobj tmpstr tmpheader tmpindex;' ];
geometry = { [1 1 1] [1 2] [nbcols] [nbcols] [nbcols] [1] [1 1 1]};
listui = { ...
{ 'style' 'text' 'string' 'Predefined format' } ...
{ 'style' 'text' 'string' 'Header lines' } ...
{ 'style' 'edit' 'tag' 'headlines' 'string' '0' 'callback' headercom } ...
{ 'style' 'listbox' 'tag' 'format' 'string' strvcat(listtype{:}) ...
'callback' formatcom 'userdata' { indexlist;formatskip } } ...
{ 'style' 'pushbutton' 'string' 'preview' ...
'callback' 'set(findobj(gcbf, ''tag'', ''Import''), ''userdata'', ''preview'')'} };
% custom columns
% --------------
for index = 1:length(nbcols)
listui{end+1} = { 'style' 'text' 'string' [ 'column ' int2str(index) ] };
end
for index = 1:length(nbcols)
listui{end+1} = { 'style' 'listbox' 'tag' [ 'col' int2str(index) ] 'string' ...
strvcat(listcolformat{:}) };
end
for index = 1:length(nbcols)
listui{end+1} = { 'style' 'text' 'string' formatstr(tmpfile(1:min(10, size(tmpfile,1)),index)) ...
'tag' ['text' int2str(index) ] 'userdata' formatstr(tmpfile(:,index)) };
end
listui = { listui{:} ...
{} ...
{ 'style' 'pushbutton' 'string' 'Cancel', 'callback', 'close gcbf;' } ...
{ 'style' 'pushbutton' 'string' 'Help', 'callback', 'pophelp(''pop_readlocs'');' } ...
{ 'style' 'pushbutton' 'string' 'Import' 'tag' 'Import' ...
'callback', 'set(findobj(gcbf, ''tag'', ''Import''), ''userdata'', ''import'')'} };
fig = figure('name', 'Importing electrode location file -- pop_readlocs()', 'visible', 'off');
supergui( fig, geometry, [1 2 1 3 7 1 1], listui{:});
% update figure
set(findobj(gcf, 'tag', 'format'), 'value', indexfiletype);
eval(formatcom);
% this loop is necessary for decoding Preview
cont = 1;
while cont
waitfor( findobj('parent', fig, 'tag', 'Import'), 'userdata');
if isempty( findobj('parent', fig, 'tag', 'Import') ), return; end
% decode inputs
% -------------
tmpheader = str2num(get(findobj(fig, 'tag', 'headlines'), 'string'));
tmpobj = findobj(fig, 'tag', 'format');
tmpformatstr = get(tmpobj, 'string');
tmpformatstr = tmpformatstr(get(tmpobj, 'value'),:);
for tmpindex=1:length(nbcols)
tmpobj = findobj(fig, 'tag', [ 'col' int2str(tmpindex) ]);
tmpcolstr{tmpindex} = get(tmpobj, 'string');
tmpcolstr{tmpindex} = deblank(tmpcolstr{tmpindex}(get(tmpobj,'value'),:));
end
% take appropriate measures
% -------------------------
res = get(findobj('parent', fig, 'tag', 'Import'), 'userdata');
set(findobj('parent', fig, 'tag', 'Import'), 'userdata', '');
if strcmp(res, 'preview')
try,
tmplocs = readlocs( filename, 'filetype', tmpformatstr, ...
'skiplines', tmpheader, 'format', tmpcolstr);
figure; topoplot([],tmplocs, 'style', 'blank', 'electrodes', 'labelpoint');
catch,
errordlg2(strvcat('Error while importing locations:', lasterr), 'Error');
end
else
cont = 0;
end
end
close(fig);
% importing files
% ---------------
tmplocs = readlocs( filename, 'filetype', tmpformatstr, ...
'skiplines', tmpheader, 'format', tmpcolstr);
else
tmplocs = readlocs( filename, varargin{:});
end
command = sprintf('EEG.chanlocs = pop_readlocs(''%s'', %s);', filename, vararg2str(varargin));
return;
% format string for text file
% ---------------------------
function alltxt = formatstr( celltxt )
for index1 = 1:size(celltxt,1)
alltxt{index1} = '';
for index2 = 1:size(celltxt,2)
alltxt{index1} = sprintf('%s\t%s', alltxt{index1}, num2str(celltxt{index1,index2}));
end
alltxt{index1} = sprintf(alltxt{index1}, '%s\n', alltxt{index1});
end
alltxt = strvcat( alltxt{:});