GetSegCoords.m
The current code for this custom MATLAB function are provided below for convenient dissemination.
GetSegCoords.m
function [xyz, hemi, segnames] = getSegCoords(varargin) % Given a set of label file names, parse the label files and return the mean XYZ coordinate of each label % Can be called with a list of parameters for easy scripting, or interactively without parameters, which will % generate a series of ui dialog boxes. % % returns: % xyz, a nx3 array of XYZ coordinates, where n is the number of labels parsed % hemi, a nx1 binary vector indicating the corresponding label is left (1) or right (0) hemisphere % segnames: a nx1 cell array of label strings. % % (C) 2018 Chris McNorgan % Use at your own risk. %
%% Part 1: Parse arguments options = struct( ...
'lhsegdir',, ... 'rhsegdir',, ... 'lsegnames', , ... 'rsegnames', , ... 'automatch', true, ... 'hemi', 'lhrh', ... 'directory', pwd() );
% read the acceptable names optionNames = fieldnames(options);
% count arguments nArgs = length(varargin); if round(nArgs/2)~=nArgs/2
error('TSExtractor needs propertyName/propertyValue pairs')
end
for pair = reshape(varargin,2,[]) % pair is {propName;propValue}
inpName = lower(pair{1}); % make case insensitive if any(strcmp(inpName,optionNames)) % overwrite options. If you want you can test for the right class here % Also, if you find out that there is an option you keep getting wrong, % you can use "if strcmp(inpName,'problemOption'),testMore,end"-statements options.(inpName) = pair{2}; else error('%s is not a recognized parameter name',inpName) end
end
lh=~isempty(strfind(options.hemi, 'lh')); rh=~isempty(strfind(options.hemi, 'rh'));
%% Part 2: Determine folders %If we want lh coords and we haven't already provided a directory, open %dialog box if (lh && isempty(options.lhsegdir))
options.lhsegdir = uigetdir(options.directory,'Select LH labels directory');
end if (rh && isempty(options.rhsegdir))
if(options.automatch) options.rhsegdir = strrep(options.lhsegdir, 'lh', 'rh'); else options.rhsegdir = uigetdir(options.directory,'Select RH labels directory'); end
end
%% Part 3: Verify segment names % If segment names were not specified, call to parseFSSegments if (lh && isempty(options.lsegnames))
%If you've indicated you want lh segment info but haven't provided the %names, prompt for them warntxt=sprintf('You have not specified the names of the lh segments.\nYou will now be prompted to locate an appropriate file (LEFT HEMISPHERE).'); d=warndlg(warntxt,'!! Warning !!'); uiwait(d); options.lsegnames=parseFSSegments();
end if (rh && isempty(options.rsegnames))
%If you've indicated you want rh segment info but haven't provided the %names, prompt for them warntxt=sprintf('You have not specified the names of the rh segments.\nYou will now be prompted to locate an appropriate file (RIGHT HEMISPHERE).'); d=warndlg(warntxt,'!! Warning !!'); uiwait(d); options.rsegnames=parseFSSegments();
end
%% Part 4: Iterate through files associated with segments xyz=nan(length(options.lsegnames)+length(options.rsegnames),3); hemi=[repmat('lh',length(options.lsegnames),1);repmat('rh',length(options.rsegnames),1)]; segnames=[(options.lsegnames);(options.rsegnames)]; i=0; if(lh)
for i=1:length(options.lsegnames) fname=fullfile(options.lhsegdir, ['lh.' options.lsegnames{i} '.label']); xyz(i,:)=getMeanLabelCoords(fname); end
end rstart=i; if(rh)
for i=1:length(options.rsegnames) fname=fullfile(options.rhsegdir, ['rh.' options.rsegnames{i} '.label']); xyz(rstart+i,:)=getMeanLabelCoords(fname); end
end
%% Helper function: Find mean XYZ of named file
function [xyz]=getMeanLabelCoords(labelfile) %First line is a header, second line is nvertices. Skip those two %and read the rest of the file: 5 columns, x y z are columns 2, 3, %and 4 (except when dlmread interprets it, the 2-space delimiters %between the columns is read as an extra column of zeros) M=dlmread(labelfile, ' ', 2, 0); x=mean(M(:,3)); y=mean(M(:,5)); z=mean(M(:,7)); xyz=[x y z]; end
end