gusucode.com > datatypes 工具箱matlab源码程序 > datatypes/array2timetable.m

    function tt = array2timetable(x,varargin)
%ARRAY2TIMETABLE Convert homogeneous array to timetable.
%   TT = ARRAY2TIMETABLE(X,'RowTimes',ROWTIMES) converts the M-by-N array X and
%   the M-by-1 datetime or duration vector ROWTIMES to an M-by-N timetable TT. Each
%   column of X becomes a variable in TT, and ROWTIMES becomes TT's time vector.
%
%   Note: to convert a table to a timetable, use TABLE2TIMETABLE.
%
%   TT = ARRAY2TIMETABLE(X,'RowTimes',ROWTIMES,'VariableNames',VARNAMES) creates
%   TT using the cell array of character vectors VARNAMES for TT's variable
%   names. The names must be valid MATLAB identifiers, and must be unique.
%
%   See also TIMETABLE, TABLE2TIMETABLE, ARRAY2TABLE, CELL2TABLE, STRUCT2TABLE.

%   Copyright 2016 The MathWorks, Inc.

if ~ismatrix(x)
    error(message('MATLAB:array2timetable:NDArray'));
end
[nrows,nvars] = size(x);
vars = mat2cell(x,nrows,ones(1,nvars));

pnames = {'VariableNames' 'RowTimes'};
dflts =  {            []         [] };
[varnamesArg,rowtimes,supplied] ...
    = matlab.internal.table.parseArgs(pnames, dflts, varargin{:});

rowtimesName = 'Time';
if ~supplied.RowTimes
    error(message('MATLAB:array2timetable:RowTimeRequired'));
end

if supplied.VariableNames
    varnames = varnamesArg;
else
    baseName = inputname(1);
    if isempty(baseName) || (nvars == 0)
        varnames = matlab.internal.table.dfltVarNames(1:nvars);
    else
        if nvars == 1
            varnames = {baseName};
        else
            varnames = matlab.internal.table.numberedNames(baseName,1:nvars);
        end
    end
end

% The input matrix defines the size of the output timetable. The time
% vector must have the same length as the matrix has rows, even if
% the matrix has no columns.
if numel(rowtimes) ~= nrows
    error(message('MATLAB:array2timetable:IncorrectNumberOfRowTimes'));
end

% Each column of x becomes a variable in t
tt = timetable(vars{:},'VariableNames',varnames,'RowTimes',rowtimes);

tt.Properties.DimensionNames{1} = rowtimesName;