time | Calls | line |
---|
| | 1 | function t = array2table(x,varargin) %#codegen
|
| | 2 | %ARRAY2TABLE Convert homogeneous array to table.
|
| | 3 | % T = ARRAY2TABLE(A) converts the M-by-N array A to an M-by-N table T.
|
| | 4 | % Each column of A becomes a variable in T.
|
| | 5 | %
|
| | 6 | % NOTE: A can be any type of array, including a cell array. However, in that
|
| | 7 | % case you probably want to use CELL2TABLE instead. ARRAY2TABLE creates the
|
| | 8 | % variables in T from each column of A. If A is a cell array, ARRAY2TABLE
|
| | 9 | % does not extract the contents of its cells -- T in this case is a table each
|
| | 10 | % of whose variables is a column of cells. To create a table from the
|
| | 11 | % contents of the cells in A, use CELL2TABLE(A).
|
| | 12 | %
|
| | 13 | % T = ARRAY2TABLE(X, 'PARAM1', VAL1, 'PARAM2', VAL2, ...) specifies optional
|
| | 14 | % parameter name/value pairs that determine how the data in X are converted.
|
| | 15 | %
|
| | 16 | % 'VariableNames' A string array or cell array of character vectors
|
| | 17 | % containing variable names for T. The names must be
|
| | 18 | % valid MATLAB identifiers, and must be unique.
|
| | 19 | % 'RowNames' A string array or cell array of character vectors
|
| | 20 | % containing row names for T. The names need not be
|
| | 21 | % valid MATLAB identifiers, but must be unique.
|
| | 22 | %
|
| | 23 | % See also TABLE2ARRAY, CELL2TABLE, STRUCT2TABLE, TABLE.
|
| | 24 |
|
| | 25 | % Copyright 2012-2019 The MathWorks, Inc.
|
| | 26 |
|
0.001 | 8 | 27 | if ~isempty(coder.target)
|
| | 28 | % codegen, redirect to codegen specific function and return
|
| | 29 | t = matlab.internal.coder.array2table(x, varargin{:});
|
| | 30 | return
|
< 0.001 | 8 | 31 | end
|
| | 32 |
|
< 0.001 | 8 | 33 | if ~ismatrix(x)
|
| | 34 | error(message('MATLAB:array2table:NDArray'));
|
< 0.001 | 8 | 35 | end
|
< 0.001 | 8 | 36 | [nrows,nvars] = size(x);
|
| | 37 |
|
< 0.001 | 8 | 38 | if nargin == 1
|
| | 39 | rownames = {};
|
| | 40 | supplied.VariableNames = false;
|
| | 41 | supplied.RowNames = false;
|
< 0.001 | 8 | 42 | else
|
< 0.001 | 8 | 43 | pnames = {'VariableNames' 'RowNames'};
|
< 0.001 | 8 | 44 | dflts = { {} {} };
|
0.014 | 8 | 45 | [varnames,rownames,supplied] ...
|
| 8 | 46 | = matlab.internal.datatypes.parseArgs(pnames, dflts, varargin{:});
|
< 0.001 | 8 | 47 | end
|
| | 48 |
|
< 0.001 | 8 | 49 | if ~supplied.VariableNames && (nvars > 0) % skip nvars==0 for performance
|
| | 50 | baseName = inputname(1);
|
| | 51 | if isempty(baseName)
|
| | 52 | varnames = matlab.internal.tabular.defaultVariableNames(1:nvars);
|
| | 53 | else
|
| | 54 | if nvars == 1
|
| | 55 | varnames = {baseName};
|
| | 56 | else
|
| | 57 | varnames = matlab.internal.datatypes.numberedNames(baseName,1:nvars);
|
| | 58 | end
|
| | 59 | end
|
< 0.001 | 8 | 60 | end
|
| | 61 |
|
< 0.001 | 8 | 62 | if nvars == 0
|
| | 63 | % Performant special case to create an Nx0 empty table.
|
| | 64 | t = table.empty(nrows,0);
|
| | 65 | % Assign the supplied var names just to check for the correct number (zero) and
|
| | 66 | % throw a consistent error. No need to check for conflicts with dim names.
|
| | 67 | if supplied.VariableNames, t.Properties.VariableNames = varnames; end
|
| | 68 | if supplied.RowNames, t.Properties.RowNames = rownames; end
|
< 0.001 | 8 | 69 | else
|
| | 70 | % Each column of X becomes a variable in T. No need to check if they are all the
|
| | 71 | % same height, they are all columns in one array.
|
0.010 | 8 | 72 | vars = mat2cell(x,nrows,ones(1,nvars));
|
0.066 | 8 | 73 | t = table.init(vars,nrows,rownames,nvars,varnames);
|
< 0.001 | 8 | 74 | end
|