Ken & Mike on the MATLAB Desktop

December 1st, 2008

Publishing Functions

fitdemo.pngUntil MATLAB R2008a, PUBLISH only worked on scripts.  One of the limitations of the MATLAB language is that you cannot define a function from within a script.  These two worked against each other to make it difficult to tell a story with M-file publishing that relied on another function.

A good workaround to this problem is to use TYPE or DBTYPE to display the secondary function. An example of this is one of the demos included in MATLAB, Optimal Fit of a Non-linear Function, or FITDEMO.  Because FMINSEARCH requires a function to optimize, FITFUN needs to be defined in a separate file and included into the narrative with TYPE.

This works fairly well, but there are a couple disadvantages to this technique.  Because FITFUN is displayed as the output from TYPE, it is just gray text without syntax highlighting.  You also now have two source M-files to manage, so functions like GRABCODE won’t work as well.

In MATLAB R2008a, we made it possible for PUBLISH to handle not just scripts, but functions too.  Converting FITDEMO into a function allows us to include FITFUN in it as a nested function.  The new version fixes both of the disadvantages mentioned above and allows greater flexibility in your M-file storytelling.

Matthew Simoneau is a developer at The MathWorks focused on technical communication and social computing.  In addition to occasional contributions to MATLAB Central, he blogs sporadically on his personal site.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Ken & Mike work on the MATLAB Desktop team.
  • DP: Hi i have a problem with ezplot3, i want to plot more than i curve in the same graph but hold on command...
  • Ken: Hi Arsalan, Unfortunately there is no way to get the new Editor API in older versions of MATLAB. -Ken
  • Arsalan: Hi, I am very excited about the MATLAB API for editor because right now i am working on a project and i need...
  • Johannes: Since I started using matlab-emacs some days ago I never experienced Emacslink. But I experienced some...
  • Francisco J. Beron-Vera: Hi all, I have recently learned about ViEmu (http://www.vimemu.c om) which, for Vi/Vim...
  • OysterEngineer: When I first learned of the Publish feature in MatLab, I thought it might be useful to help to...
  • Ken: Hi Herve, I’m not quite sure what you mean by “stand-alone&# 8221; mode? -Ken
  • Herve: I wonder when the publish fonction will be supported in standalone mode.
  • Mike: Ravi, What you described should work as far I understand it. Please follow up with technical support. With a...
  • Mike: @Daniel, Thanks for that note.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.