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

    function [groupCode,msg] = variableEditorGroupCode(this,varName,startCol,endCol)
% This function is for internal use only and will change in a
% future release.  Do not use this function.

% Generate MATLAB command to group variables in column range startCol to
% endCol

%   Copyright 2013-2016 The MathWorks, Inc.

msg = '';
[varNames,varIndices] = variableEditorColumnNames(this);

if isdatetime(this.rowDim.labels) || isduration(this.rowDim.labels)
    % varNames and varIndices include the rownames, if they are datetimes
    % or duration.  These aren't needed for the group function.
    varNames(1) = [];
    varIndices(1) = [];
    varIndices = varIndices-1;
    % startCol and endCol also includes the time column, so decrement it
    startCol = startCol-1;
    endCol = endCol-1;
end

startIndex = find(varIndices(1:end-1)<=startCol,1,'last');
endIndex = find(varIndices(1:end-1)<=endCol,1,'last');

% If startIndex is not the first, begin with a subsref for the 
% first index-1 table variables.
if startIndex>1 
    groupCode = [varName ' = [' varName '(:,1:' num2str(startIndex-1) '),'];
else
    groupCode = [varName ' = ['];
end

secondCmd = '';
firstParam = '';
if isduration(this.rowDim.labels) || isdatetime(this.rowDim.labels)
    % Keep the row labels consistent by adding them as the first argument
    % to the new object
    dimName = this.Properties.DimensionNames{1};
    firstParam = [varName '.' dimName ', '];
    
    % Make sure dimension names remain the same as well
    secondCmd = [' ' varName '.Properties.DimensionNames{1} = ''' dimName ''';'];
end

groupCode = [groupCode class(this) '(' firstParam '['];

% Add code to insert a table for the grouped columns
% e.g., table([x.variableName1,x.variableName2,...])
groupedVarName = '';
for k=startIndex:endIndex
    groupedVarName = sprintf('%s%s',groupedVarName,varNames{k});
    groupCode = sprintf('%s%s.%s',groupCode,varName,varNames{k});
    if k<endIndex
       groupCode = sprintf('%s,',groupCode);
       groupedVarName = sprintf('%s_',groupedVarName);
    end   
end
groupCode = [groupCode '], ''VariableNames'',{''' matlab.lang.makeUniqueStrings(matlab.lang.makeValidName(groupedVarName), {}, namelengthmax) '''})'];

% Add the part of the table after the grouped table variables
if endIndex<size(this,2)
    groupCode = [groupCode ',' varName '(:,' num2str(endIndex+1) ':end)];'];
else
    groupCode = [groupCode '];'];
end

if ~isempty(secondCmd)
    % Add in the second command if it is set
    groupCode = [groupCode secondCmd];
end