gusucode.com > datatypes 工具箱matlab源码程序 > datatypes/@tabular/private/setProperty.m

    function t = setProperty(t,name,p)
%SETPROPERTY Set a table property.

%   Copyright 2012-2016 The MathWorks, Inc.

% We may be given a name (when called from set), or a subscript expression
% that starts with a '.name' subscript (when called from subsasgn).  Get the
% name and validate it in any case.
if isstruct(name)
    s = name;
    if s(1).type ~= '.'
        error(message('MATLAB:table:InvalidSubscript'));
    end
    name = s(1).subs;
    haveSubscript = true;
else
    haveSubscript = false;
end
% Allow partial match for property names if this is via the set method;
% require exact match if it is direct assignment via subsasgn
name = matchPropertyName(name,t.propertyNames,haveSubscript);

if haveSubscript && ~isscalar(s)
    % If this is 1-D named parens/braces subscripting, convert labels to 
    % correct indices for properties that support subscripting with labels. 
    % e.g. t.Properties.RowNames('SomeRowName')
    if ~strcmp(s(2).type,'.') && isscalar(s(2).subs)
        sub = s(2).subs{1};
        if matlab.internal.datatypes.isCharStrings(sub) % a name, names, or colon
            switch name
            case {'VariableNames' 'VariableDescriptions' 'VariableUnits'}
                s(2).subs{1} = t.varDim.subs2inds(sub);
            case {'RowNames' 'RowTimes'}
                s(2).subs{1} = t.rowDim.subs2inds(sub);
            case 'DimensionNames'
                s(2).subs{1} = t.metaDim.subs2inds(sub);
            end
        end
    end
    % If there's cascaded subscripting into the property, get the existing
    % property value and let the property's subsasgn handle the assignment.
    % The property may currently be empty, ask for a non-empty default
    % version to allow assignment into only some elements. Guarantee correct
    % dispatch for the assignment by working inside a scalar cell and letting
    % built-in cell subscripting dispatch.
    LHScell = { getProperty(t,name,true) };
    s(1).type = '{}'; s(1).subs = {1};
    LHScell = builtin('subsasgn',LHScell,s,p);
    p = LHScell{1};
    % The assignment may change the property's shape or size or otherwise make
    % it invalid; that gets checked by the individual setproperty methods called
    % below.
end

% Assign the new property value into the dataset.
switch name
case {'RowNames' 'RowTimes'}
    t.rowDim = t.rowDim.setLabels(p); % error if duplicate, or empty
case 'VariableNames'
    t.varDim = t.varDim.setLabels(p); % error if invalid, duplicate, or empty
    % Check for conflicts between the new VariableNames and the existing
    % DimensionNames. For backwards compatibility, a table will modify
    % DimensionNames and warn, while a timetable will error.
    t.metaDim = t.metaDim.checkAgainstVarLabels(t.varDim.labels);
case 'DimensionNames'
    t.metaDim = t.metaDim.setLabels(p); % error if duplicate, or empty
    % Check for conflicts between the new DmensionNames and the existing
    % VariableNames. For backwards compatibility, a table will modify
    % DimensionNames and warn, while a timetable will error.
    t.metaDim = t.metaDim.checkAgainstVarLabels(t.varDim.labels);
case 'VariableDescriptions'
    t.varDim = t.varDim.setDescrs(p);
case 'VariableUnits'
    t.varDim = t.varDim.setUnits(p);
case 'Description'
    t = setDescription(t,p);
case 'UserData'
    t = setUserData(t,p);
end