%% Obtain Statistics for Memoized Function
% Create a MemoizedFunction object by memozing the plus function.

mf = memoize(@plus)

% Call the memoized function several times.  The first time you call the function with a particular set of inputs, MATLAB caches the results.

a = mf(13,42);  % calls plus; caches results
b = mf(7,33);   % calls plus; caches results
c = mf(13,42);  % does not call plus; returns cached results
d = mf(5,120);  % calls plus; caches results
e = mf(13,42);  % does not call plus; returns cached results
f = mf(7,33);   % does not call plus; returns cached results

% Call the stats function.

s = stats(mf)

% Determine which sets of inputs you used the most.

s.MostHitCachedInput

% Obtain the cached results.

c = s.Cache

% Display all sets of cached inputs and outputs alongside how many times MATLAB accessed the cached values.

T = cell2table([c.Inputs' c.Outputs'],'VariableNames',{'Inputs','Outputs'});
T.Times_Cache_Accessed = c.HitCount'

% Copyright 2012 The MathWorks, Inc.