time | Calls | line |
---|
| | 1 | function varIndices = getVarOrRowLabelIndices(t,varSubscripts,allowEmptyRowLabels)
|
| | 2 | % Translate the specified variable specification, which may include the row labels
|
| | 3 | % name, into var indices, with 0 indicating row labels. This is useful in cases
|
| | 4 | % where the var spec is an input arg to methods like sortrows or varfun, but is
|
| | 5 | % not appropriate for processing subscripts in parens/braces subscripting where
|
| | 6 | % the row labels name is never legal. The only way the row labels can be
|
| | 7 | % specified in a var spec is by name, never by numeric or logical var indices.
|
| | 8 |
|
| | 9 | % Copyright 2016-2019 The MathWorks, Inc.
|
| | 10 |
|
| | 11 | % Check to see if we have var names (as opposed to indices or logical).
|
0.002 | 3 | 12 | haveVarNames = matlab.internal.datatypes.isText(varSubscripts);
|
| | 13 |
|
< 0.001 | 3 | 14 | if haveVarNames % only way row labels can be specified
|
| | 15 | % Separate out any names that specify the row labels from those that
|
| | 16 | % specify data vars.
|
| | 17 | isRowLabels = matches(varSubscripts,t.metaDim.labels{1});
|
| | 18 |
|
| | 19 | % Add the row labels to the list of vars in the output.
|
| | 20 | rowLabelsIncluded = any(isRowLabels);
|
| | 21 | if rowLabelsIncluded
|
| | 22 | if t.rowDim.hasLabels || ((nargin > 2) && allowEmptyRowLabels)
|
| | 23 | if isscalar(isRowLabels)
|
| | 24 | % If it's only the row labels, return quickly. This also prevents a
|
| | 25 | % char row vector from causing problems below.
|
| | 26 | varIndices = 0;
|
| | 27 | return
|
| | 28 | end
|
| | 29 |
|
| | 30 | % Preallocate the outputs, data var elements to be overwritten later.
|
| | 31 | varIndices = zeros(1,length(varSubscripts));
|
| | 32 |
|
| | 33 | isDataVar = ~isRowLabels;
|
| | 34 | dataVarSubscripts = varSubscripts(isDataVar);
|
| | 35 | else
|
| | 36 | throwAsCaller(t.throwSubclassSpecificError('NoRowLabels'));
|
| | 37 | end
|
| | 38 | else
|
| | 39 | dataVarSubscripts = varSubscripts;
|
| | 40 | end
|
< 0.001 | 3 | 41 | else
|
< 0.001 | 3 | 42 | rowLabelsIncluded = false;
|
< 0.001 | 3 | 43 | dataVarSubscripts = varSubscripts;
|
< 0.001 | 3 | 44 | end
|
| | 45 |
|
| | 46 | % Validate data var subscripts and translate them into numeric indices.
|
< 0.001 | 3 | 47 | try
|
0.012 | 3 | 48 | dataVarIndices = t.varDim.subs2inds(dataVarSubscripts);
|
| | 49 | catch ME
|
| | 50 | if ME.identifier == "MATLAB:table:UnrecognizedVarName"
|
| | 51 | rowDimName = t.metaDim.labels{1};
|
| | 52 | defaultRowDimName = t.defaultDimNames{1};
|
| | 53 | dataVarSubscripts = cellstr(dataVarSubscripts);
|
| | 54 | if matches(defaultRowDimName,dataVarSubscripts)
|
| | 55 | % Helpful error if an unrecognized var name specifies the default row
|
| | 56 | % dim name 'Row'/'Time', but the actual row dim name has been renamed
|
| | 57 | % to something other than the default.
|
| | 58 | ME = t.throwSubclassSpecificError('RowDimNameNondefault',defaultRowDimName,rowDimName);
|
| | 59 | elseif matches(rowDimName,dataVarSubscripts,"IgnoreCase",true)
|
| | 60 | % Helpful error if an unrecognized var name is the row dim name, just
|
| | 61 | % off by case.
|
| | 62 | match = find(matches(dataVarSubscripts,rowDimName,"IgnoreCase", true),1);
|
| | 63 | ME = t.throwSubclassSpecificError('RowDimNameCase',dataVarSubscripts{match},rowDimName);
|
| | 64 | else
|
| | 65 | [tf,loc] = ismember(lower(dataVarSubscripts),lower(t.varDim.labels));
|
| | 66 | tf(ismember(dataVarSubscripts,t.varDim.labels)) = false;
|
| | 67 | match = find(tf,1);
|
| | 68 | if ~isempty(match)
|
| | 69 | % Helpful error if an unrecognized var name is just off by case.
|
| | 70 | attempt = dataVarSubscripts{match};
|
| | 71 | actual = t.varDim.labels{loc(match)};
|
| | 72 | ME = MException(message('MATLAB:table:UnrecognizedVarNameCase',attempt,actual));
|
| | 73 | end
|
| | 74 | end
|
| | 75 | end
|
| | 76 | throwAsCaller(ME);
|
< 0.001 | 3 | 77 | end
|
| | 78 |
|
| | 79 | % Add data vars to the list of vars in the output.
|
< 0.001 | 3 | 80 | if rowLabelsIncluded
|
| | 81 | varIndices(isDataVar) = dataVarIndices;
|
< 0.001 | 3 | 82 | else
|
< 0.001 | 3 | 83 | varIndices = dataVarIndices;
|
< 0.001 | 3 | 84 | end
|
Other subfunctions in this file are not included in this listing.