%% Read Datastore of MAT-Files
% Create a datastore containing all the .mat files within the MATLAB® demos folder, specifying the load function to read the file data.

fds = fileDatastore(fullfile(matlabroot,"toolbox","matlab","demos"),"ReadFcn",@load,"FileExtensions",".mat")

%
% Read the first file in the datastore, and then read the second file.

data1 = read(fds);                   
data2 = read(fds);

%
% Read all files in the datastore simultaneously.

readall(fds);

%
% Initialize a cell array to hold the data and counter i.

dataarray = cell(numel(fds.Files), 1);
i = 1;

%
% Reset the datastore to the first file and read the files one at a time until there is no data left. Assign the data to the array dataarray.

reset(fds);                          
while hasdata(fds)                   
    dataarray{i} = read(fds);
    i = i+1;
end

% Copyright 2020 The MathWorks, Inc.