%% Read and Analyze MAT-File with Key-Value Data
% This example shows how to create a datastore for key-value pair data in a MAT-file that is the output of mapreduce. Then, the example shows how to read all the data in the datastore and sort it. This example assumes that the data in the MAT-file fits in memory.
% Create a datastore from the sample file, mapredout.mat, using the datastore function. The sample file contains unique keys representing airline carrier codes and corresponding values that represent the number of flights operated by that carrier.

ds = datastore('mapredout.mat');

% datastore returns a KeyValueDatastore. The datastore function automatically determines the appropriate type of datastore to create.
%
% Preview the data using the preview function. This function does not affect the state of the datastore.

preview(ds)

%
% Read all of the data in ds using the readall function. The readall function returns a table with two columns, Key and Value.

T = readall(ds)

% T contains all the airline and flight data from the datastore in the same order in which the data was read. The table variables, Key and Value, are cell arrays.
%
% Convert Value to a numeric array.

T.Value = cell2mat(T.Value)

%
% Assign new names to the table variables.

T.Properties.VariableNames = {'Airline','NumFlights'};

%
% Sort the data in T by the number of flights.

T = sortrows(T,'NumFlights','descend')

%
% View a summary of the sorted table.

summary(T)

%
% Reset the datastore to allow rereading of the data.

reset(ds)

% Copyright 2015 The MathWorks, Inc.