gusucode.com > datatypes 工具箱matlab源码程序 > datatypes/+matlab/+internal/+tableUtils/defaultarrayLike.m

    function y = defaultarrayLike(sz,~,x)
%DEFAULTARRAYLIKE Create a variable like x containing null values
%   Y = DEFAULTARRAYLIKE(SZ,'Like',X) returns a variable the same class as X,
%   with the specified size, containing default values.  The default value for
%   floating point types is NaN, in other cases the default value is the value
%   MATLAB uses by default to fill in unspecified elements on array expansion.
%
%      Array Class            Null Value
%      ---------------------------------------------
%      double, single         NaN
%      duration               NaN
%      calendarDuration       NaN
%      datetime               NaT
%      int8, ..., uint64      0
%      logical                false
%      categorical            <undefined>
%      char                   char(0)
%      cellstr                {''}
%      cell                   {[]}
%      string                 string('')
%      other                  [MATLAB default value]

%   Copyright 2012-2013 The MathWorks, Inc.

n = sz(1); p = prod(sz(2:end));
if isfloat(x)
    y = nan(sz,'like',x);
elseif isnumeric(x)
    y = zeros(sz,'like',x);
elseif islogical(x)
    y = false(sz);
elseif isa(x,'categorical')
    y = x(1:0);
    if n*p > 0
        y(n,p) = categorical.undefLabel;
    end
    y = reshape(y,sz);
elseif isa(x, 'datetime') 
    y = NaT(sz);
    y.Format = x.Format;
    y.TimeZone = x.TimeZone;
    
% duration and calendarDuration fill with 0, set to NaN explicitly
elseif isa(x, 'duration') 
    y = duration(NaN(sz),0,0);
    y.Format = x.Format;
elseif isa(x, 'calendarDuration') 
    y = calendarDuration(NaN(sz),0,0);
    y.Format = x.Format;
    
elseif iscell(x)
    if iscellstr(x)
        y = repmat({''},sz);
    else
        y = cell(sz);
    end
elseif ischar(x)
    y = repmat(char(0),sz);
elseif isstring(x)
    y = repmat(string(nan),sz);
else % fallback for unrecognized types
    y = x(1:0); % create an emoty versioj of the input
    if n*p > 0
        % If the output is non-empty, assign a value just past the desired end
        % to fill the previous element with their default values.
        y(n*p+1) = x(1); % this will fail for empty x
    end
    y = reshape(y(1:n*p),sz);% reshape the default elements to the output size
end