time | Calls | line |
---|
| | 1 | function [varargout] = getProperty(t,name,createIfEmpty)
|
| | 2 | %GETPROPERTY Get a table property.
|
| | 3 |
|
| | 4 | % Copyright 2012-2019 The MathWorks, Inc.
|
| | 5 |
|
| | 6 | import matlab.tabular.Continuity
|
| | 7 | import matlab.internal.datatypes.isColon
|
| | 8 |
|
< 0.001 | 6 | 9 | if nargin < 3, createIfEmpty = false; end
|
| | 10 |
|
| | 11 | % We may be given a name (when called from get), or a subscript expression
|
| | 12 | % that starts with a '.name' subscript (when called from subsref). Get the
|
| | 13 | % name and validate it in any case.
|
< 0.001 | 6 | 14 | if isstruct(name)
|
< 0.001 | 4 | 15 | s = name;
|
< 0.001 | 4 | 16 | if s(1).type == '.'
|
< 0.001 | 4 | 17 | name = s(1).subs;
|
| | 18 | else
|
| | 19 | error(message('MATLAB:table:InvalidSubscript'));
|
< 0.001 | 4 | 20 | end
|
< 0.001 | 4 | 21 | haveSubscript = true;
|
< 0.001 | 2 | 22 | else
|
< 0.001 | 2 | 23 | haveSubscript = false;
|
< 0.001 | 6 | 24 | end
|
| | 25 | % Allow partial match for property names if this is via the get method;
|
| | 26 | % require exact match if it is via subsref
|
< 0.001 | 6 | 27 | name = tabular.matchPropertyName(name,t.propertyNames,haveSubscript);
|
< 0.001 | 6 | 28 | isPerVarCustomProp = false;
|
| | 29 |
|
| | 30 | % Get the property out of the table. Some properties need special handling
|
| | 31 | % when empty: create either a non-empty default version or a "canonical" 0x0
|
| | 32 | % cell array (subscripting can sometimes make them 1x0 or 0x1), depending on
|
| | 33 | % what the caller asks for.
|
< 0.001 | 6 | 34 | switch name
|
< 0.001 | 6 | 35 | case {'RowNames' 'RowTimes'}
|
| | 36 | if t.rowDim.hasLabels || ~createIfEmpty
|
| | 37 | p = t.rowDim.labels;
|
| | 38 | else
|
| | 39 | p = t.rowDim.defaultLabels();
|
| | 40 | end
|
| | 41 |
|
| | 42 | % These three have already been verified present by matchPropertyName
|
< 0.001 | 6 | 43 | case 'StartTime'
|
| | 44 | p = t.rowDim.startTime;
|
< 0.001 | 6 | 45 | case 'TimeStep'
|
| | 46 | p = t.rowDim.timeStep;
|
< 0.001 | 6 | 47 | case 'SampleRate'
|
| | 48 | p = t.rowDim.sampleRate;
|
| | 49 |
|
< 0.001 | 6 | 50 | case 'VariableNames'
|
< 0.001 | 4 | 51 | p = t.varDim.labels;
|
| | 52 | % varnames are "always there", so leave them 1x0 when empty
|
< 0.001 | 2 | 53 | case 'DimensionNames'
|
| | 54 | p = t.metaDim.labels;
|
< 0.001 | 2 | 55 | case 'VariableDescriptions'
|
< 0.001 | 2 | 56 | p = t.varDim.descrs;
|
< 0.001 | 2 | 57 | if ~t.varDim.hasDescrs && createIfEmpty
|
| | 58 | p = repmat({''},1,t.varDim.length);
|
< 0.001 | 2 | 59 | end
|
| | 60 | case 'VariableUnits'
|
| | 61 | p = t.varDim.units;
|
| | 62 | if ~t.varDim.hasUnits && createIfEmpty
|
| | 63 | p = repmat({''},1,t.varDim.length);
|
| | 64 | end
|
| | 65 | case 'VariableContinuity'
|
| | 66 | p = t.varDim.continuity;
|
| | 67 | if ~t.varDim.hasContinuity && createIfEmpty
|
| | 68 | p = repmat(Continuity.unset,1,t.varDim.length);
|
| | 69 | end
|
| | 70 | case 'Description'
|
| | 71 | p = t.arrayProps.Description;
|
| | 72 | case 'CustomProperties'
|
| | 73 | % Construct CustomProperties object from per-var and per-table. Avoid
|
| | 74 | % constructing the object if it's not needed.
|
| | 75 | if ~haveSubscript || isscalar(s)
|
| | 76 | p = matlab.tabular.CustomProperties(t.arrayProps.TableCustomProperties, t.varDim.customProps);
|
| | 77 | elseif s(2).type ~= '.'
|
| | 78 | if s(2).type == "{}"
|
| | 79 | error(message('MATLAB:table:CustomProperties:CellReferenceNotAllowed'))
|
| | 80 | else % '()'
|
| | 81 | error(message('MATLAB:table:CustomProperties:ParensReferenceNotAllowed'))
|
| | 82 | end
|
| | 83 | else
|
| | 84 | % Get the particular custom property. Cascading subscripting
|
| | 85 | % happens later.
|
| | 86 | customPropName = s(2).subs;
|
| | 87 | if isfield(t.varDim.customProps, customPropName) % per-variable custom property
|
| | 88 | isPerVarCustomProp = true;
|
| | 89 | p = t.varDim.customProps.(customPropName);
|
| | 90 | elseif isfield(t.arrayProps.TableCustomProperties, customPropName) % per-table custom property
|
| | 91 | p = t.arrayProps.TableCustomProperties.(customPropName);
|
| | 92 | else
|
| | 93 | error(message('MATLAB:table:UnrecognizedCustomProperty',customPropName))
|
| | 94 | end
|
| | 95 | % Peel off the first layer of subscripting because it's already
|
| | 96 | % done.
|
| | 97 | s = s(2:end);
|
| | 98 | end
|
| | 99 | case 'UserData'
|
| | 100 | p = t.arrayProps.UserData;
|
< 0.001 | 6 | 101 | end
|
| | 102 |
|
< 0.001 | 6 | 103 | if haveSubscript && ~isscalar(s)
|
| | 104 | % If this is 1-D named parens/braces subscripting, convert labels to
|
| | 105 | % correct indices for properties that support subscripting with labels.
|
| | 106 | % e.g. t.Properties.VariableUnits('SomeVarName')
|
| | 107 | if (name ~="UserData") && ~(name == "CustomProperties" && ~isPerVarCustomProp) % fend off UserData and per-table CustomProperties
|
| | 108 | if (s(2).type ~= ".") && isscalar(s(2).subs) % () or {}
|
| | 109 | sub = s(2).subs{1};
|
| | 110 | % Call subs2inds on all subscript types to get nice error
|
| | 111 | % handling.
|
| | 112 | switch name
|
| | 113 | case {'VariableNames' 'VariableDescriptions' 'VariableUnits' 'VariableContinuity'}
|
| | 114 | % Most subs2inds callers want a colon expanded out, here we don't.
|
| | 115 | if isColon(sub)
|
| | 116 | inds = sub;
|
| | 117 | else
|
| | 118 | inds = t.varDim.subs2inds(sub);
|
| | 119 | end
|
| | 120 | case 'RowNames'
|
| | 121 | inds = t.rowDim.subs2inds(sub);
|
| | 122 | case 'DimensionNames'
|
| | 123 | inds = t.metaDim.subs2inds(sub);
|
| | 124 | case 'CustomProperties'
|
| | 125 | inds = t.varDim.subs2inds(sub);
|
| | 126 | end
|
| | 127 | % subs2inds returns the indices as row/col/col vectors, but a
|
| | 128 | % table's properties aren't "on the grid", and so should follow the usual
|
| | 129 | % reshaping rules for subscripting. One (char) name and colon are fine as
|
| | 130 | % is, but preserve a cellstr subscript's original shape.
|
| | 131 | if matlab.internal.datatypes.isText(sub) % a name, names, or colon
|
| | 132 | if iscell(sub), inds = reshape(inds,size(sub)); end
|
| | 133 | s(2).subs{1} = inds;
|
| | 134 | end
|
| | 135 | end
|
| | 136 | % If there's cascaded subscripting into the property, let the property's
|
| | 137 | % subsasgn handle the reference. This may return a comma-separated list,
|
| | 138 | % so ask for and assign to as many outputs as we're given. If there's no
|
| | 139 | % LHS to the original expression (nargout==0), this only assigns one
|
| | 140 | % output and drops everything else in the CSL.
|
| | 141 | [varargout{1:nargout}] = subsref(p,s(2:end));
|
| | 142 |
|
| | 143 | else %'UserData' or 'CustomProperties.<per-table-prop>'
|
| | 144 | % If there's a table in the UserData struct, need to be able to access both builtin
|
| | 145 | % and overloaded subscripting.
|
| | 146 | [varargout{1:nargout}] = matlab.internal.tabular.private.subsrefRecurser(p,s(2:end));
|
| | 147 | end
|
| | 148 |
|
< 0.001 | 6 | 149 | else
|
| | 150 | % If there's no cascaded subscripting, only ever assign the property itself.
|
< 0.001 | 6 | 151 | varargout{1} = p;
|
< 0.001 | 6 | 152 | end
|
Other subfunctions in this file are not included in this listing.