%% Read Collection or Sequence of Text Files
% When your data is stored across multiple text files, you can use tabularTextDatastore to manage and import the data. This example shows how to use tabularTextDatastore to read the data from the collection of text files all together, or to read one file at a time.
%% Data
% For this example, the folder C:\DataTxt contains a collection of text files. Capture this location in the variable location. The data  contains 10 text files, where each file contains 10 rows of data. The results differ based on your files and data.

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

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

ds = tabularTextDatastore(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, you can import it all at once using the readall function.

allData = readall(ds);
size(allData)

% Alternatively, 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. Set the ReadSize to 'file' or a positive integer.
% If ReadSize is 'file', then each call to read reads all the data one file at a time.
% If ReadSize is a positive integer, then each call to read reads 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.
%
%