www.gusucode.com > datastoreio工具箱 matlab源码程序 > datastoreio/+matlab/+io/+datastore/@TabularTextDatastore/readAllData.m

    function data = readAllData(ds)
%READALLDATA Read all of the data from a TabularTextDatastore.
%   T = READALLDATA(TDS) reads all of the data from TDS.
%   T is a table with variables governed by TDS.SelectedVariableNames.
%
%   Example:
%   --------
%      % Create a TabularTextDatastore
%      tabds = tabularTextDatastore('airlinesmall.csv')
%      % Handle erroneous data
%      tabds.TreatAsMissing = 'NA';
%      tabds.MissingValue = 0;
%      % We are only interested in the Arrival Delay data
%      tabds.SelectedVariableNames = 'ArrDelay'
%      tab = readall(tabds);
%      sumAD = sum(tab.ArrDelay)
%
%   See also - matlab.io.datastore.TabularTextDatastore, hasdata, read, preview, reset.

%   Copyright 2016 The MathWorks, Inc.

try    
    reset(ds);
    
    % If empty files return an empty table with correct SelectedVariableNames
    if isEmptyFiles(ds) || ~hasdata(ds)
        data = emptyTable(ds, ds.SelectedVariableNames);
        return;
    end
    
    % resets and sets the old ReadSize value
    currReadSize = ds.ReadSize;
    cleanup = onCleanup(@()cleanupDatastore(ds, currReadSize));
    
    % estimate max rows per read by num variables
    ds.ReadSize = max( 1, floor(4e6/numel(ds.VariableNames)) );
    
    tblCells = cell(1, ds.Splitter.NumSplits);
    readIdx = 1;
    while hasdata(ds)
        tblCells{readIdx} = read(ds);
        readIdx = readIdx + 1;
    end
    
    data = vertcat(tblCells{:});
    delete(cleanup);
catch ME
    throw(ME);
end
end

function cleanupDatastore(ds, readSize)
    % safe to reset, as reset works for empty files as well
    ds.ReadSize = readSize;
    reset(ds);
end