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

    function t = array2table(x,varargin)
%ARRAY2TABLE Convert matrix to table.
%   T = ARRAY2TABLE(A) converts the M-by-N array A to an M-by-N table T.
%   Each column of A becomes a variable in T.
%
%   NOTE:  A can be any type of array, including a cell array.  However, in that
%   case you probably want to use CELL2TABLE instead.  ARRAY2TABLE creates the
%   variables in T from each column of A.  If A is a cell array, ARRAY2TABLE
%   does not extract the contents of its cells -- T in this case is a table each
%   of whose variables is a column of cells.  To create a table from the
%   contents of the cells in A, use CELL2TABLE(A).
%
%   T = ARRAY2TABLE(X, 'PARAM1', VAL1, 'PARAM2', VAL2, ...) specifies optional
%   parameter name/value pairs that determine how the data in X are converted.
%
%      'VariableNames'  A cell array of character vectors containing
%                       variable names for T.  The names must be valid
%                       MATLAB identifiers, and must be unique.
%      'RowNames'       A cell array of character vectors containing row
%                       names for T.  The names need not be valid MATLAB
%                       identifiers, but must be unique.
%
%   See also TABLE2ARRAY, CELL2TABLE, STRUCT2TABLE, TABLE.

%   Copyright 2012-2016 The MathWorks, Inc.

if ~ismatrix(x)
    error(message('MATLAB:array2table:NDArray'));
end
[nrows,nvars] = size(x);

pnames = {'VariableNames' 'RowNames'};
dflts =  {            []         [] };
[varnames,rownames,supplied] ...
    = matlab.internal.table.parseArgs(pnames, dflts, varargin{:});

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

vars = mat2cell(x,nrows,ones(1,nvars));

if isempty(vars) 
    % creating an empty table with the correct dimensions
    t = table.empty(nrows,nvars);
else
    % Each column of x becomes a variable in t
    t = table(vars{:});
end

if supplied.RowNames, t.Properties.RowNames = rownames;      end
if haveVarNames,      t.Properties.VariableNames = varnames; end