GetSegCoords.m: Difference between revisions
Jump to navigation
Jump to search
(Created page with "The current code for this custom MATLAB function are provided below for convenient dissemination. =GetSegCoords.m= function [xyz, hemi, segnames] = getSegCoords(varargin) %...") |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
The current code for this custom MATLAB function | The current code for this custom MATLAB function is provided below for convenient dissemination. | ||
=GetSegCoords.m= | =GetSegCoords.m= | ||
function [xyz, hemi, segnames] = getSegCoords(varargin) | 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 | % 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 | % Can be called with a list of parameters for easy scripting, or interactively without parameters, which will | ||
% generate a series of ui dialog boxes. | % generate a series of ui dialog boxes. | ||
% | % | ||
% returns: | % returns: | ||
% xyz, a nx3 array of XYZ coordinates, where n is the number of labels parsed | % 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 | % hemi, a nx1 binary vector indicating the corresponding label is left (1) or right (0) hemisphere | ||
% segnames: a nx1 cell array of label strings. | % segnames: a nx1 cell array of label strings. | ||
% | % | ||
% (C) 2018 Chris McNorgan | % (C) 2018 Chris McNorgan | ||
% Use at your own risk. | % Use at your own risk. | ||
% | % | ||
%% Part 1: Parse arguments | %% Part 1: Parse arguments | ||
options = struct( ... | options = struct( ... | ||
'lhsegdir','', ... | 'lhsegdir','', ... | ||
'rhsegdir','', ... | 'rhsegdir','', ... | ||
Line 26: | Line 26: | ||
'hemi', 'lhrh', ... | 'hemi', 'lhrh', ... | ||
'directory', pwd() ); | 'directory', pwd() ); | ||
% read the acceptable names | % read the acceptable names | ||
optionNames = fieldnames(options); | optionNames = fieldnames(options); | ||
% count arguments | % count arguments | ||
nArgs = length(varargin); | nArgs = length(varargin); | ||
if round(nArgs/2)~=nArgs/2 | if round(nArgs/2)~=nArgs/2 | ||
error('TSExtractor needs propertyName/propertyValue pairs') | error('TSExtractor needs propertyName/propertyValue pairs') | ||
end | end | ||
for pair = reshape(varargin,2,[]) % pair is {propName;propValue} | for pair = reshape(varargin,2,[]) % pair is {propName;propValue} | ||
inpName = lower(pair{1}); % make case insensitive | inpName = lower(pair{1}); % make case insensitive | ||
Line 47: | Line 47: | ||
error('%s is not a recognized parameter name',inpName) | error('%s is not a recognized parameter name',inpName) | ||
end | end | ||
end | end | ||
lh=~isempty(strfind(options.hemi, 'lh')); | lh=~isempty(strfind(options.hemi, 'lh')); | ||
rh=~isempty(strfind(options.hemi, 'rh')); | rh=~isempty(strfind(options.hemi, 'rh')); | ||
%% Part 2: Determine folders | %% Part 2: Determine folders | ||
%If we want lh coords and we haven't already provided a directory, open | %If we want lh coords and we haven't already provided a directory, open | ||
%dialog box | %dialog box | ||
if (lh && isempty(options.lhsegdir)) | if (lh && isempty(options.lhsegdir)) | ||
options.lhsegdir = uigetdir(options.directory,'Select LH labels directory'); | options.lhsegdir = uigetdir(options.directory,'Select LH labels directory'); | ||
end | end | ||
if (rh && isempty(options.rhsegdir)) | if (rh && isempty(options.rhsegdir)) | ||
if(options.automatch) | if(options.automatch) | ||
options.rhsegdir = strrep(options.lhsegdir, 'lh', 'rh'); | options.rhsegdir = strrep(options.lhsegdir, 'lh', 'rh'); | ||
Line 64: | Line 64: | ||
options.rhsegdir = uigetdir(options.directory,'Select RH labels directory'); | options.rhsegdir = uigetdir(options.directory,'Select RH labels directory'); | ||
end | end | ||
end | end | ||
%% Part 3: Verify segment names | %% Part 3: Verify segment names | ||
% If segment names were not specified, call to parseFSSegments | % If segment names were not specified, call to parseFSSegments | ||
if (lh && isempty(options.lsegnames)) | if (lh && isempty(options.lsegnames)) | ||
%If you've indicated you want lh segment info but haven't provided the | %If you've indicated you want lh segment info but haven't provided the | ||
%names, prompt for them | %names, prompt for them | ||
Line 75: | Line 75: | ||
uiwait(d); | uiwait(d); | ||
options.lsegnames=parseFSSegments(); | options.lsegnames=parseFSSegments(); | ||
end | end | ||
if (rh && isempty(options.rsegnames)) | if (rh && isempty(options.rsegnames)) | ||
%If you've indicated you want rh segment info but haven't provided the | %If you've indicated you want rh segment info but haven't provided the | ||
%names, prompt for them | %names, prompt for them | ||
Line 83: | Line 83: | ||
uiwait(d); | uiwait(d); | ||
options.rsegnames=parseFSSegments(); | options.rsegnames=parseFSSegments(); | ||
end | end | ||
%% Part 4: Iterate through files associated with segments | %% Part 4: Iterate through files associated with segments | ||
xyz=nan(length(options.lsegnames)+length(options.rsegnames),3); | xyz=nan(length(options.lsegnames)+length(options.rsegnames),3); | ||
hemi=[repmat('lh',length(options.lsegnames),1);repmat('rh',length(options.rsegnames),1)]; | hemi=[repmat('lh',length(options.lsegnames),1);repmat('rh',length(options.rsegnames),1)]; | ||
segnames=[(options.lsegnames);(options.rsegnames)]; | segnames=[(options.lsegnames);(options.rsegnames)]; | ||
i=0; | i=0; | ||
if(lh) | if(lh) | ||
for i=1:length(options.lsegnames) | for i=1:length(options.lsegnames) | ||
fname=fullfile(options.lhsegdir, ['lh.' options.lsegnames{i} '.label']); | fname=fullfile(options.lhsegdir, ['lh.' options.lsegnames{i} '.label']); | ||
xyz(i,:)=getMeanLabelCoords(fname); | xyz(i,:)=getMeanLabelCoords(fname); | ||
end | end | ||
end | end | ||
rstart=i; | rstart=i; | ||
if(rh) | if(rh) | ||
for i=1:length(options.rsegnames) | for i=1:length(options.rsegnames) | ||
fname=fullfile(options.rhsegdir, ['rh.' options.rsegnames{i} '.label']); | fname=fullfile(options.rhsegdir, ['rh.' options.rsegnames{i} '.label']); | ||
xyz(rstart+i,:)=getMeanLabelCoords(fname); | xyz(rstart+i,:)=getMeanLabelCoords(fname); | ||
end | end | ||
end | end | ||
%% Helper function: Find mean XYZ of named file | %% Helper function: Find mean XYZ of named file | ||
function [xyz]=getMeanLabelCoords(labelfile) | function [xyz]=getMeanLabelCoords(labelfile) | ||
%First line is a header, second line is nvertices. Skip those two | %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 read the rest of the file: 5 columns, x y z are columns 2, 3, | ||
Line 117: | Line 117: | ||
xyz=[x y z]; | xyz=[x y z]; | ||
end | end | ||
end | end |
Latest revision as of 12:17, 6 November 2018
The current code for this custom MATLAB function is 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