File Exchange

image thumbnail

exportToZip: Identifies the dependencies of an M-file and creates a ZIP file

version (3.62 KB) by Malcolm Wood
Files inside $matlabroot/toolbox are omitted, providing a useful way to package your work


Updated 01 Sep 2016

View Version History

View License

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

The function "mydepfun" determines the dependencies of an
M-file, ignoring files inside the MATLAB "toolbox" directory. Skipping these files gives significant performance improvements over calling "depfun" directly, especially in MATLAB installations which include multiple toolboxes. The assumption is the same as that in "mypath.m", i.e. that most of us do our work in directories outside $matlabroot/toolbox.
The function "exportToZip" creates a ZIP file containing all the dependencies identified by "mydepfun" for one or more MATLAB functions. This simplifies the process of passing your work to another user, or uploading it to MATLAB Central, by reducing both the effort required and the likelihood of a file being missed out.

The limitations of "depfun" apply: for example, that dependencies introduced by calls such as "eval('peaks')" are not identified. An additional limitation is that if files in the MATLAB toolbox directory somehow depend on files *outside* that directory (e.g.
if you have overloaded toolbox functions), these dependencies will not be picked up as they would by "depfun" itself.

The directory hierarchy is preserved by "exportToZip", which means that all required files must be on the same drive so that a "common root directory" can be determined.

Cite As

Malcolm Wood (2021). exportToZip: Identifies the dependencies of an M-file and creates a ZIP file (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (21)

Karl Kastner

This script relies on the function depfun which was removed with Matlab release v2016a.

Anurak Thungtong

Great tool. Your functions save a lot of my time. Thank you for sharing ^___^

Xavier Gelabert

In addition to my previous comment,
line: newlist = newlist(2:end); % first entry is always the same file again.
should also be changed by:
newlist=newlist(~ strcmpi(newlist,toscan{1}));
in mydepfun.

Thomas Null

As Joerg said, line 70, but transpose the output.
list = depfun(func,'-toponly','-quiet');
list = matlab.codetools.requiredFilesAndProducts(func,'toponly').';


Great tool, but not compatible with matlab version > 2014 or functions not in the same disk. Joerg's solution does not work for me either. I look forward to an update. Thanks.

larryb bliven


Great work, cool tool.

I am using Matlab 2015b and
DEPFUN has been removed.

To get it work, replace call to depfun() in file "mydepfun.m" with "matlab.codetools.requiredFilesAndProducts()"

in file "mydepfun.m", line 70
<- list = depfun(func,'-toponly','-quiet');
+> list = matlab.codetools.requiredFilesAndProducts(func,'toponly');


Makes packing m files so easy.


This should really be part of Matlab -- it Just Works.

Just a quick shout out: if you want to include object-orientated code in your .zip, by default depfun and "mydepfun" won't find private methods of your objects. This is because they'll never be on your path (by definition) and you'll get errors like "error using depfun > next_arg_file. The file 'privateMethod' does not exist".

The solution to this is to change line 39 of exportToZip to read

req{i} = matlab.codetools.requiredFilesAndProducts(funcname{i});

which can handle objects properly.

Simply great

Michael Völker

Tobias Kienzler

great work! but you should change

if ~isempty(dirname) && dirname(1)=='@'


if ~isempty(dirname) && (dirname(1)=='@' || dirname(1)=='+')

to handle packages correctly as well

Thomas Vanaret

In function "mydepfun", subfunction "i_function_name" may be corrected as following to treat correctly "private" directories :

function func = i_function_name(f)
% Identifies the function name for the specified file,
% including the class name where appropriate. Does not
% work for UDD classes, e.g. @rtw/@rtw

[dirname,funcname] = fileparts(f);
[ignore,dirname] = fileparts(dirname);

% [TV] patch for 'private' functions
if strcmp(dirname, 'private')
[ignore, private_parent] = fileparts(ignore);
dirname = private_parent;
funcname = [ 'private' filesep funcname ];
% /[TV] patch for 'private' functions

if ~isempty(dirname) && dirname(1)=='@'
func = [ dirname filesep funcname ];
func = funcname;

Thomas Vanaret

"private" directories not treated by "i_function_name" subfunction, what causes errors / incorrect results

See "getFileDependencies" in Matlab FEX

per isakson

exportToZip cannot handle packages(?)

??? Error using ==> depfun>next_arg_file at 1423
The file 'MemmapAdapter' does not exist.
Error in ==> mydepfun>i_scan at 69
list = depfun(func,'-toponly','-quiet');

where 'MemmapAdapter' is an m-file in a package.

Is "packages" on your TODO-list?



Brett Shoelson

I second mut ante's observation, and his suggestion for improvement. This function, which has earned a spot on my shortcut bar, fails in certain cases because of case sensitivity. I replaced all instances of STRNCMP with STRNCMPI in subfunction i_root_directory, and the problem went away.

Thierry Dalon

Comment on Tim Tam comment:
look at the option 'recursive' of mydepfun.
exporttoZip uses this option so you get all dependencies.

Tim Tam

mydepfun does not appear to find dependencies of dependencies ? When I used ML's depfun it returned 30 non-toolbox m files (plus one custom DLL), while your mydepfun returned only 13 m files.

Murphy O'Brien

Great! This is just what I needed. It's perfect for releasing code to a user and also a handy way of making a new directory without all the useless files that somehow get created on the way.

mut ante

hi: i have the habit of mixing upper and lower case when writing important folder names... the function worked properlly after changing the string comparison functions to the corresponding ignore case functions... well this is admissible in a win system, but.... thx for this function

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!