Commit 252d875a authored by mikec1's avatar mikec1
Browse files

Added an internal strsplit style function.

parent 83d98442
......@@ -37,8 +37,9 @@
function out=pop_rfind(projRoot, searchRoot)
out = '';
split = strsplit(projRoot,'/'); % Get project name.
split = bc_strsplit(projRoot,'/'); % Get project name.
projName = split(end);
filePattern = fullfile(searchRoot, 'sub-*'); % Search for BIDS pattern.
allFiles = dir(filePattern);
......@@ -59,10 +60,124 @@ function out=pop_rfind(projRoot, searchRoot)
ind = find(ismember(reSplit,projName));
finalPath = '';
for i = ind+1 : length(reSplit) % Matlab doesn't have a cell2str...
finalPath = sprintf('%s%s',finalPath, reSplit{i});
finalPath = sprintf('%s%s/',finalPath, reSplit{i});
end
% Build return variable.
out = sprintf('%s%s\n',out,finalPath);
out = sprintf('%s%s\n',out,finalPath(1:end-1));
end
end
% THIS FUNCTION IS ONLY HERE TO USE THIS PARTICULAR TYPE OF STRSPLIT
% WITHOUT HAVING TO INRODUCE ANOTHER FILE WHICH MAY CONFUSE PEOPLE OR ALLOW
% AN UNSAFE DEPENDENCY.
function terms = bc_strsplit(s, delimiter)
%STRSPLIT Splits a string into multiple terms
%
% terms = strsplit(s)
% splits the string s into multiple terms that are separated by
% white spaces (white spaces also include tab and newline).
%
% The extracted terms are returned in form of a cell array of
% strings.
%
% terms = strsplit(s, delimiter)
% splits the string s into multiple terms that are separated by
% the specified delimiter.
%
% Remarks
% -------
% - Note that the spaces surrounding the delimiter are considered
% part of the delimiter, and thus removed from the extracted
% terms.
%
% - If there are two consecutive non-whitespace delimiters, it is
% regarded that there is an empty-string term between them.
%
% Examples
% --------
% % extract the words delimited by white spaces
% ts = strsplit('I am using MATLAB');
% ts <- {'I', 'am', 'using', 'MATLAB'}
%
% % split operands delimited by '+'
% ts = strsplit('1+2+3+4', '+');
% ts <- {'1', '2', '3', '4'}
%
% % It still works if there are spaces surrounding the delimiter
% ts = strsplit('1 + 2 + 3 + 4', '+');
% ts <- {'1', '2', '3', '4'}
%
% % Consecutive delimiters results in empty terms
% ts = strsplit('C,Java, C++ ,, Python, MATLAB', ',');
% ts <- {'C', 'Java', 'C++', '', 'Python', 'MATLAB'}
%
% % When no delimiter is presented, the entire string is considered
% % as a single term
% ts = strsplit('YouAndMe');
% ts <- {'YouAndMe'}
%
% History
% -------
% - Created by Dahua Lin, on Oct 9, 2008
%
%% parse and verify input arguments
assert(ischar(s) && ndims(s) == 2 && size(s,1) <= 1, ...
'strsplit:invalidarg', ...
'The first input argument should be a char string.');
if nargin < 2
by_space = true;
else
d = delimiter;
assert(ischar(d) && ndims(d) == 2 && size(d,1) == 1 && ~isempty(d), ...
'strsplit:invalidarg', ...
'The delimiter should be a non-empty char string.');
d = strtrim(d);
by_space = isempty(d);
end
%% main
s = strtrim(s);
if by_space
w = isspace(s);
if any(w)
% decide the positions of terms
dw = diff(w);
sp = [1, find(dw == -1) + 1]; % start positions of terms
ep = [find(dw == 1), length(s)]; % end positions of terms
% extract the terms
nt = numel(sp);
terms = cell(1, nt);
for i = 1 : nt
terms{i} = s(sp(i):ep(i));
end
else
terms = {s};
end
else
p = strfind(s, d);
if ~isempty(p)
% extract the terms
nt = numel(p) + 1;
terms = cell(1, nt);
sp = 1;
dl = length(delimiter);
for i = 1 : nt-1
terms{i} = strtrim(s(sp:p(i)-1));
sp = p(i) + dl;
end
terms{nt} = strtrim(s(sp:end));
else
terms = {s};
end
end
end
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment