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

    function data = readAllData(ds)
%READALLDATA Read all of the data from a SpreadsheetDatastore.
%   T = READALLDATA(SSDS) reads all of the data from SSDS.
%   T is a table with variables governed by SSDS.SelectedVariableNames.
%
%   Example:
%   --------
%      % Create a SpreadsheetDatastore
%      ssds = spreadsheetDatastore('airlinesmall_subset.xlsx')
%      % We are only interested in the Arrival Delay data
%      ssds.SelectedVariableNames = 'ArrDelay'
%      % read all the data
%      tab = readall(ssds);
%
%   See also - matlab.io.datastore.SpreadsheetDatastore, hasdata, readall, 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));
        
        % set ReadSize to 'file'
        ds.ReadSize = 'file';
        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