📄 splittocell.m
字号:
function [linetocell, cellcount] = splittocell(line, delimiter, varargin)
% splittocell - split a delimited string into a cell array
%
% usage is straight forward:
%
% FORMAT: [out, cnt] = splittocell(string [,delims, multi, anydelim])
%
% Input fields:
% string 1xN char array to split
% delims char array containing one or more delimiters
% if left empty -> char(9) == <TAB>
% multi must be '1' (numeric) to be effective, if set
% multiple delimiters will be treated as one
% anydelim match any of the delimiters (for multi-char
% delimiters strings)
%
% Output fields:
% out cell array containing the tokens after split
% cnt number of tokens in result
%
% See also gluetostring.
% Version: v0.5c
% Build: 6120415
% Date: Dec-04 2006, 3:15 PM CET
% Author: Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools
% argument check
if nargin < 1
error( ...
'BVQXtools:TooFewArguments', ...
'Too few arguments. Try ''help %s''.', ...
mfilename ...
);
end
% initialize return values and varargin{3}
linetocell = cell(0);
cellcount = 0;
multidelim = false;
anydelim = false;
% do we have useful input ?
if isempty(line), return; end
if ~ischar(line) || ...
size(line, 2) ~= numel(line)
error( ...
'BVQXtools:BadArgument', ...
'Input must be a 1xN shaped char array!' ...
);
end
% reset cellcount
cellcount = 1;
% are any other arguments specified
if nargin < 2 || ...
~ischar(delimiter)
delimiter = char(9);
else
delimiter = delimiter(:)';
end
if nargin > 2 && ...
~isempty(varargin{1}) && ...
(isnumeric(varargin{1}) || ...
islogical(varargin{1})) && ...
varargin{1}(1)
multidelim = true;
end
if nargin > 3 && ...
~isempty(varargin{2}) && ...
(isnumeric(varargin{2}) || ...
islogical(varargin{2})) && ...
varargin{2}(1)
anydelim = true;
end
% set initial parameters
lline = size(line, 2);
ldelim = size(delimiter, 2);
% standard approach
if ~anydelim
if ldelim < lline
if strcmp(line(end+1-ldelim:end), delimiter)
cpos = [(1 - ldelim), strfind(line, delimiter)];
else
cpos = [(1 - ldelim), strfind(line, delimiter), lline + 1];
end
elseif ldelim == lline
if strcmp(line, delimiter)
linetocell = {''};
else
linetocell = {line};
end
return;
else
linetocell = {line};
return;
end
% any of the given delimiters (e.g. white spaces)
else
% last char is a delimiter
if ~any(delimiter == line(end))
cpos = [0, lline + 1];
else
cpos = 0;
end
% get all delimiter positions
for pchar = delimiter
cpos = union(cpos, strfind(line, pchar));
end
% set ldelim to 1!
ldelim = 1;
end
% number of delimiters
lcpos = length(cpos);
% any delimiter found at all ?
if lcpos < 2
error( ...
'BVQXtools:InternalError', ...
'Error working with pattern.' ...
);
elseif lcpos == 2
linetocell = {line(cpos(1) + ldelim:cpos(2) - 1)};
return;
end
% concatenate in case of multidelims
ecpos = cpos(2:end) - 1;
if multidelim
if ecpos(1) == 0
mcpos = [1, find(diff(ecpos) <= ldelim) + 1];
else
mcpos = find(diff(ecpos) <= ldelim) + 1;
end
cpos(mcpos) = [];
ecpos(mcpos) = [];
end
cpos = cpos + ldelim;
ncpos = length(ecpos);
cellcount = ncpos;
linetocell = cell(1, ncpos);
% extract substrings
for dpos = 1:ncpos
linetocell{dpos} = line(cpos(dpos):ecpos(dpos));
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -