%% Read Collection or Sequence of Spreadsheet Files
% When you have data stored across multiple spreadsheet files, use spreadsheetDatastore to manage and import the data. After creating the datastore, you can read all the data from the collection simultaneously, or you can read one file at a time.
%% Data
% If the folder C:\Data contains a collection of spreadsheet files, then capture the location of the data in location. The data used in this example contains 10 spreadsheet files, where each file contains 10 rows of data. Your results will differ based on your files and data.

location = 'C:\Data';
dir(location)

%% Create Datastore
% Create a datastore using the location of the files.

ds = spreadsheetDatastore(location)

% Read Data from Datastore
% Use the read or readall functions to import the data from the datastore. If the data from the collection fits in the memory, then you can import it all at once using the readall function.

allData = readall(ds);
size(allData)

% Alternatively, you can import the data one file at a time using the read function. To control the amount of data imported, before you call read, adjust the ReadSize property of the datastore. You can set the ReadSize to 'file', 'sheet', or a positive integer.
% If ReadSize is 'file', then each call to read returns data one file at a time.
% If ReadSize is 'sheet', then each call to read returns data one sheet at a time.
% If ReadSize is a positive integer, then each call to read returns the number of rows specified by ReadSize, or fewer if it reaches the end of the data.

ds.ReadSize = 'file';
firstFile = read(ds) % reads first file
secondFile = read(ds) % reads second file

% Copyright 2018 The MathWorks, Inc.