time | Calls | line |
---|
| | 1 | function [b,varargout] = subsrefParens(t,s)
|
| | 2 | %SUBSREFPARENS Subscripted reference for a table.
|
| | 3 |
|
| | 4 | % Copyright 2012-2019 The MathWorks, Inc.
|
| | 5 |
|
| | 6 | % '()' is a reference to a subset of a table. If no subscripting
|
| | 7 | % follows, return the subarray. Only dot subscripting may follow.
|
| | 8 |
|
| | 9 | import matlab.internal.datatypes.isUniqueNumeric
|
0.048 | 70595 | 10 | subsType = matlab.internal.tabular.private.tabularDimension.subsType;
|
| | 11 |
|
0.004 | 70595 | 12 | if ~isstruct(s), s = substruct('()',s); end
|
| | 13 |
|
0.198 | 70595 | 14 | if numel(s(1).subs) ~= t.metaDim.length
|
| | 15 | error(message('MATLAB:table:NDSubscript'));
|
0.003 | 70595 | 16 | elseif isscalar(s) && nargout > 1
|
| | 17 | % Simple parenthesis indexing can only return a single thing.
|
| | 18 | error(message('MATLAB:table:TooManyOutputs'));
|
0.003 | 70595 | 19 | end
|
| | 20 |
|
| | 21 | % Create an empty output table.
|
1.812 | 70595 | 22 | b = t.cloneAsEmpty(); % respect the subclass
|
| | 23 |
|
| | 24 | % Translate row labels into indices (leaves logical and ':' alone).
|
0.034 | 70595 | 25 | t_rowDim = t.rowDim;
|
2.316 | 70595 | 26 | [rowIndices,numRowIndices,~,isColonRows,~,b_rowDim] = t_rowDim.subs2inds(s(1).subs{1},subsType.reference);
|
0.452 | 70595 | 27 | b.rowDim = b_rowDim;
|
| | 28 |
|
| | 29 | % Translate variable (column) names into indices (translates logical and ':').
|
0.026 | 70595 | 30 | t_varDim = t.varDim;
|
1.519 | 70595 | 31 | [varIndices,numVarIndices,~,~,~,b_varDim] = t_varDim.subs2inds(s(1).subs{2},subsType.reference,t.data);
|
0.595 | 70595 | 32 | b.varDim = b_varDim;
|
| | 33 |
|
| | 34 | % Move the data to the output.
|
0.141 | 70595 | 35 | b_data = cell(1,b.varDim.length);
|
0.025 | 70595 | 36 | t_data = t.data;
|
0.006 | 70595 | 37 | for j = 1:numVarIndices
|
0.379 | 764633 | 38 | var_j = t_data{varIndices(j)};
|
0.040 | 764633 | 39 | if isColonRows
|
| | 40 | b_data{j} = var_j; % a fast shared-data copy
|
1.413 | 764633 | 41 | elseif isa(var_j,'tabular')
|
| | 42 | b_data{j} = var_j.subsrefParens({rowIndices ':'}); % force dispatch to overloaded table subscripting
|
0.458 | 764633 | 43 | elseif ismatrix(var_j)
|
2.536 | 764633 | 44 | b_data{j} = var_j(rowIndices,:); % without using reshape, may not have one
|
| | 45 | else
|
| | 46 | % Each var could have any number of dims, no way of knowing,
|
| | 47 | % except how many rows they have. So just treat them as 2D to get
|
| | 48 | % the necessary rows, and then reshape to their original dims.
|
| | 49 | sizeOut = size(var_j); sizeOut(1) = numRowIndices;
|
| | 50 | b_data{j} = reshape(var_j(rowIndices,:), sizeOut);
|
0.028 | 764633 | 51 | end
|
0.041 | 764633 | 52 | end
|
0.084 | 70595 | 53 | b.data = b_data;
|
| | 54 |
|
| | 55 | % Create subscripters for the output. If the RHS subscripts are labels or numeric
|
| | 56 | % indices, they may have picked out the same row or variable more than once, but
|
| | 57 | % selectFrom creates the output labels correctly.
|
0.107 | 70595 | 58 | b.metaDim = t.metaDim;
|
| | 59 |
|
| | 60 | % Move the per-array properties to the output.
|
0.092 | 70595 | 61 | b.arrayProps = t.arrayProps;
|
| | 62 |
|
0.005 | 70595 | 63 | if isscalar(s)
|
| | 64 | % If there's no additional subscripting, return the subarray.
|
0.004 | 70595 | 65 | if nargout > 1
|
| | 66 | nargoutchk(0,1);
|
0.004 | 70595 | 67 | end
|
| | 68 | else
|
| | 69 | switch s(2).type
|
| | 70 | case '()'
|
| | 71 | error(message('MATLAB:table:InvalidSubscriptExpr'));
|
| | 72 | case '{}'
|
| | 73 | error(message('MATLAB:table:InvalidSubscriptExpr'));
|
| | 74 | case '.'
|
| | 75 | % subsrefParens's output args are defined as [b,varargout] so the
|
| | 76 | % nargout==1 case can avoid varargout, although that adds complexity to
|
| | 77 | % the nargout==0 case. See detailed comments in subsref.
|
| | 78 | if nargout == 1
|
| | 79 | b = b.subsrefDot(s(2:end)); % b is a table, dispatch directly to subsrefDot
|
| | 80 | elseif nargout > 1
|
| | 81 | [b, varargout{1:nargout-1}] = b.subsrefDot(s(2:end));
|
| | 82 | else % nargout == 0
|
| | 83 | % Let varargout bump magic capture either one output or zero
|
| | 84 | % outputs. See detailed comments in subsref.
|
| | 85 | [varargout{1:nargout}] = b.subsrefDot(s(2:end)); % ditto
|
| | 86 | if isempty(varargout)
|
| | 87 | % There is nothing to return, remove the first output arg.
|
| | 88 | clear b
|
| | 89 | else
|
| | 90 | % Shift the return value into the first output arg.
|
| | 91 | b = varargout{1};
|
| | 92 | varargout = {}; % never any additional values
|
| | 93 | end
|
| | 94 | end
|
| | 95 | end
|
0.194 | 70595 | 96 | end
|
Other subfunctions in this file are not included in this listing.