File Exchange

image thumbnail


version 1.5 (8.61 KB) by

Cache the results of slow function calls

1 Download


View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

CACHEDCALL can be used to cache the results of slow function calls to disk

You can call an arbitrary function using cachedcall. It will then check
if you have made the same call with the same arguments before. If you
have then it will quickly load the previously computed results from the
disk cache rather than calculating them again. If there are no matching
calls in the cache, then the results will be calculated and saved in the

USAGE: [a,b,...]=cachedcall(fun[,Arguments,parameter,value])

fun: handle to function whose outputs should be cached.
arguments: a cell with all the arguments that should be passed to fun.

Optional Named Parameters:
CacheFolder: the folder where the results are cached. The default folder
is the system temporary files folder (see tempdir)
MaxCacheSize: The maximum size of the cache before oldest cached
results will be deleted. (default=3e9)
SaveArguments: (default=false) this can be used to also save the input
arguments passed to the function in the cache
MaxAge: discards cache if older than maxage - units are days.
Tag: A custom string that can be saved in the cache. displayed
when inspecting (default a string describing contents of Arguments)

Further usage:
* cachedcall clear - will empty the entire cache directory.
* cachedcall inspect - will inspect the contents of the cache.
* cachedcall clean - will delete expired contents of the cache.
Expired means older than MaxAge or the results
of a function that has changed

Example 1:
tic,isp1=isprime(x); normalcalltime=toc
tic,isp2=cachedcall(@isprime,x); firstcachedcalltime=toc
tic,isp3=cachedcall(@isprime,x); secondcachedcalltime=toc

Example 2: cache the results of retrieving data from the web

Relies on: DataHash by Jan Simon (included in download).
Improved performance if TypeCastX by James Tursa installed (not required).

Aslak Grinsted 2015

Comments and Ratings (4)

Jan Simon

Jan Simon (view profile)

Modern Matlab versions have an efficient TYPECAST method. So you could use the leaner new version of DataHash. But if an MD5 sum is enough, GetMD5 is much faster.

Thanks for the submission, I have been waiting for this functionality.

These are some suggestions to make its usage safer:
- optional warning/message if cached data is used
- optional hashing of m-files (maybe with a configurable depth of m-files that are called within the function), to catch cases in which the called m-file was modified

Aslak Grinsted

Aslak Grinsted (view profile)

@JanSimon: I have updated the code to your new version of DataHash (with my modifications).

Jan Simon

Jan Simon (view profile)

Please notice that there will be a new version of DataHash with some bugfixes published in the next days.



Updated to include newest version of DataHash


Introduced maxage parameter


wrote a better explanation of what it does.


improved description / improved nargout handling / changed datahash to include filedate & filesize in the hash of function_handles.


* Improved inspect command with links
* implemented nargout==-1 workaround for some inline functions.

MATLAB Release
MATLAB 8.0 (R2012b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video