%% Write and Reconstruct Datastore
% Write a datastore to disk, and then create a new datastore for the written files. This process is useful for saving your work or sharing a datastore with a colleague.
%
% Create a datastore for the airlinesmall.csv data set, treating 'NA' values as missing data. Select only the Year, Month, and Carrier variables.

ds = datastore('airlinesmall.csv');
ds.TreatAsMissing = 'NA';
ds.SelectedVariableNames = {'Month','Year','UniqueCarrier'};

%
% Preview the datastore.

preview(ds)

%
% Save this datastore to a new folder named ExampleData on the C:\ disk. (You can specify a different write location, especially if you are not using a Windows® computer.) Specify 'FolderLayout' as 'flatten' to put the datastore files directly in the target folder, without creating subfolders.

location = 'C:\ExampleData';
writeall(ds,location,'Folderlayout','flatten')

%
% Clear ds from your working directory. To recover the datastore from disk, create a new datastore that references the same folder where the data was written.

clear ds
ds2 = datastore(location);

%
% Copyright 2019 The MathWorks, Inc.