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

    function b = extractData(t,vars)
%EXTRACTDATA Extract data from a table.
%   B = EXTRACTDATA(T,VARS) returns the contents of the variables table T
%   specified by VARS, converted to an array whose type is that of the first
%   variable. The classes of the remaining variables must support the
%   conversion. VARS is a positive integer, a vector of positive integers, a
%   variable name, a cell array containing one or more variable names, or a
%   logical vector.
%
%   See also TABLE.

%   Copyright 2012-2016 The MathWorks, Inc.

import matlab.internal.tableUtils.defaultarrayLike

vars = t.varDim.subs2inds(vars);
if isempty(vars)
    b = zeros(t.rowDim.length,0,'double');
    return
end
varsData = t.data(vars);

dims = cellfun('ndims',varsData);
if any(diff(dims)) % verify all veriables have same number of dimensions
    error(message('MATLAB:table:ExtractDataDimensionMismatch'));
end
sizes = zeros(length(varsData), dims(1));
for i = 1:length(varsData)
    sizes(i,:) = size(varsData{i});
end
if any(any(diff(sizes(:,[1 3:end]),1),1))
    error(message('MATLAB:table:ExtractDataSizeMismatch'));
end

areCells = false(1,length(varsData));
for i = 1:length(varsData)
    areCells(i) = isa(varsData{i},'cell');
end
[~,firsts] = unique(areCells,'first'); % first non-cell, first cell
if length(firsts) > 1
    % Do not concatenate cell variables with non-cell variables.  Do not do
    % what the built-in would try to do.  Give a specific error.
    j = vars(firsts);
    error(message('MATLAB:table:ExtractDataIncompatibleTypeError', ...
        t.varDim.labels{j(1)},t.varDim.labels{j(2)},class(t.data{j(1)}),class(t.data{j(2)})));
end
try
    b = [ varsData{:} ];
catch ME
    if strcmp(ME.identifier,'MATLAB:UnableToConvert')
        % cell/non-cell has already been weeded out.  The built-in errors only
        % for char/logical, or for struct/anything.  Give a specific error.
        firsts = [find(cellfun(@(x)isa(x,'char'),varsData),1,'first') ...
                  find(cellfun(@(x)isa(x,'logical'),varsData),1,'first') ...
                  find(cellfun(@(x)isa(x,'struct'),varsData),1,'first')];
        if length(firsts) > 1
            j = vars(sort(firsts));
            error(message('MATLAB:table:ExtractDataIncompatibleTypeError', ...
                t.varDim.labels{j(1)},t.varDim.labels{j(2)},class(t.data{j(1)}),class(t.data{j(2)})));
        end
    end
    % Either it wasn't the builtin that errored, or somethign unexpected
    % happened.  Give a less specific error.
    m = message('MATLAB:table:ExtractDataCatError');
    throw(addCause(MException(m.Identifier,'%s',getString(m)),ME));
end