gusucode.com > bigdata 工具箱 matlab源码程序 > bigdata/@tall/strrep.m

    function out = strrep(in, oldSubstr, newSubstr)
%STRREP Replace string with another.
%   MODIFIEDSTR = STRREP(ORIGSTR,OLDSUBSTR,NEWSUBSTR)
%
%   Limitations:
%   ORIGSTR must be a tall array of strings or a cell array of strings,
%   OLDSTR and NEWSTR must be a single string or a similarly sized tall
%   array of strings.

%   Copyright 2015-2016 The MathWorks, Inc.

narginchk(3,3);
nargoutchk(0,1);

% We require that the first input is the tall array and the others are
% plain strings or similarly sized tall arrays of strings
if ~istall(in)
    error(message('MATLAB:bigdata:array:StrrepNotTall'))
end
in = validateAndMaybeWrap(in, mfilename, 1);
oldSubstr = validateAndMaybeWrap(oldSubstr, mfilename, 2);
newSubstr = validateAndMaybeWrap(newSubstr, mfilename, 3);

% All inputs are cellstr or strings so we can use element-wise rules
out = elementfun(@strrep, in, oldSubstr, newSubstr);

% Copy the adaptor from the input so that the output has the correct class
% and calculate the new dimensions.
out.Adaptor = resetSizeInformation(in.Adaptor);
out = computeElementwiseSize(out, {in, oldSubstr, newSubstr});

end % strrep


function arg = validateAndMaybeWrap(arg, fcnName, argIdx)
% Check a substring input to make sure it is valid. Allowed types are:
% * a char row vector
% * a scalar string
% * a scalar cell containing a char array
% * a tall string array or cellstr the same length as the first input
%
% Tall arguments are validated lazily and must be arrays. Non-tall
% arguments are validated immediately and then broadcast so that they can
% be passed directly to STRREP.

if istall(arg)
    arg = tall.validateType(arg, fcnName, {'string', 'cellstr'}, argIdx);
else
    if ~isValidString(arg)
        error(message('MATLAB:strrep:InvalidInputType'));
    end
    % If the argument is char-array, wrap in a cell so that we don't try and
    % expand dimensions
    if ischar(arg)
        arg = {arg};
    end
end
end % validateAndMaybeWrap