File Exchange

image thumbnail

Publish dependent and called functions

version 1.6 (4.55 KB) by

Publish explicitly called functions not found in the MATLAB root. See mfile for limitations.

7 Ratings



View License

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

  publishdepfun creates a single published html file with called
  functions attached to the end of the root mfile. Hyperlinks will be
  within a "Called Functions" in the Table of Contents area of the
  published document that link to the called function. Functions that are
  called that are under MATLABROOT are not included in the published
  publishdepfun only goes one level deep....
  Example: If fun1 calls fun2, fun2 will be published. If fun2 calls fun3,
           fun3 will not be published.
  Calling: new_doc = publishdepfun(func,opts)
  * func => character string of root function or script ('Function1').
            The ".m" is not needed.
  * opts => is the list of options for the publishing. See publish.m help
            for more information.
 * add_fun => is a cell array of additional functions to be published.
 * rm_fun => is a cell array of functions to remove from publish list.
  * new_doc => the final html document with attached subfunctions.
                    Name is the root file name with "_withfuncs" appended.

Comments and Ratings (23)

Matt Falcy

This example from MATLAB documentation fixed my problem:

%% External File Content Example
% This example includes the file contents of sine_wave.m into published
% output.
% <include>sine_wave.m</include>
% The file content above is properly syntax highlighted

Matt Falcy

Using 2016b, I have the same error as Muhammad. I implemented Ricardo's suggestions and obtained this error:

Error using cell2struct
Invalid field name "\\Kalawatseti\home\falcym\docs\Projects\People\Seals\Redband201"
Error in publish (line 65)
    options = cell2struct(pv(2:2:end),pv(1:2:end),2);
Error in publishdepfun (line 132)
Error in Redband2016 (line 1)

Is possible to obtain the pdf?

Suggestions of changes in publishdepfun.m in order to use matlab.codetools.requiredFilesAndProducts instead of depfun (with the new command dependencies are listed in alphabetical order and the root function may be in any position).

line 103:
list = matlab.codetools.requiredFilesAndProducts(func,'toponly');

line 129:
funcId = find(not(cellfun('isempty', strfind(out, func))));

lines 135:137:
%for i=2:length(out)
% out_doc{i}=publish(out{i},options);
for i=1:funcId-1
for i=funcId+1:length(out)

Using 2016b and I got these:
ror using depfun (line 4)
DEPFUN has been removed. Use matlab.codetools.requiredFilesAndProducts instead.

Error in publishdepfun (line 103)

Error in publish_all_example (line 9)

Works perfectly!

Ting Zhou

Says depfun was removed. Any ideas?

YX Sun

YX Sun (view profile)


Hi, is there a way to avoid multiple publishing of subfunctions that are called multiple times? Let's say for instance that I have created an auxiliary function that needs to be called twice with different argumetns. This way your tool will list it in the Called Functions twice and will attach it at the end of the document twice.
Thank you.

Nick Angelini

Nick Angelini (view profile)

@jon there is not going to be a pdf version. This is simply spliced html code together. MATLAB uses a completely different method for creating pdfs. If you want pdfs look at the "Publish The Main And The Subfunctions Together". It creates a new mfile with all your functions then publishes it.

I don't know what you mean by doesn't publish first script alongside your functions.

The example makes two files. It creates a published file of all the files it thinks it needs and copies code from them into the "withfuncs" file.

If you think the code is not working properly either give me more info or send me the files so I can look at it.


jon (view profile)

Close but doesn't have pdf or publish my first script alongside my functions.

I have two separate files.. myfile & myfilewithfuncs. They are not a single file like the example makes.

I have two functions in separate scripts along with my main file. I also have one inline function.

Nick Angelini

Nick Angelini (view profile)

Updated to include ability to remove functions from publish list.

I can look into adding pdf output if I have time. It would take some effort considering I am splicing this together with html code and the default publish function does not create html code before making the pdf.


This is great. I was wandering though if there is a way to suppress a particular function file? I am running a student version and I do not have the toolbox that includes the roundn function, so I am using one found here. I would rather this function not be published in order to keep my overall report looking better.

SeungGyu cho

you're a jewel.

Wang Jing

Great job! It will be perfect if the output can be pdf.


Thanks for this code! It helped me to combine everything nicely for my programming class.

However, is there a way to combine everything into a .pdf? I'm currently copying everything published in HTML to Word and saving as a .pdf.

Jiro Doke

Jiro Doke (view profile)


Thanks for adding the web output in case of no output argument. "nargout" is the appropriate way to deal with that.


Arvind (view profile)

Thanks Nick... This is exactly what I needed

Nick Angelini

Nick Angelini (view profile)


Thanks for finding that. I have added it to the file and have submitted the updated version.


Mikhail (view profile)


However, it also includes my called MEX functions into the generated report (which looks kinda weird in the report).

I added the following around line 95 to mitigate this particular behavior:

%% Remove hits that are probably MEX functions
for i=1:length(idx),
    idxs(i) = ~isempty(idx{i});

Nick Angelini

Nick Angelini (view profile)


I have added some crude support for displaying the html document when no output argument is given. I just used nargout. Is there a more elegant solution?

Jiro Doke

Jiro Doke (view profile)

Very nice entry Nick!

This may be about preference, but it would be nice if I call the function without any output argument, then it would open the published HTML in the MATLAB browser. This is the same behavior as "publish".

Otherwise, I really like it. I plan on highlighting this entry for this week's Pick of the Week.

Thanks for the submission!



added ability to add user list of functions that are not to be published


added ability to remove mex files from output


Added support for displaying published html document if no output variable is specified.


Added support for displaying published html document if no output variable is specified.


Changed title


Version 2
# added ability to pass additional functions as a cell array to be published.
# added '-calltree' opition to the depfun call to find the functions
passed into other functions like ode's and quad.

MATLAB Release
MATLAB 7.13 (R2011b)

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

» Watch video