%% Import Text from Multiple Files Using a File Datastore
% If your text data is contained in multiple files in a folder, then you can import the text data into MATLAB using a file datastore.
% Create a file datastore for the example sonnet text files. The examples sonnets have file names "exampleSonnetN.txt", where N is the number of the sonnet. Specify the read function to be extractFileText.

readFcn = @extractFileText;
fds = fileDatastore('exampleSonnet*.txt','ReadFcn',readFcn)

% Create an empty bag-of-words model.

bag = bagOfWords

% Loop over the files in the datastore and read each file. Tokenize the text in each file and add the document to bag.

while hasdata(fds)
    str = read(fds);
    document = tokenizedDocument(str);
    bag = addDocument(bag,document);
end

% View the updated bag-of-words model.

bag

% Copyright 2018 The MathWorks, Inc.