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.
  • Ori: The current folder shortcut used to be alt-y. Now it is alt-o. However, while in the editor window, alt-o opens...
  • Jose Miguel: Hi First of all, thanks for the post, it is really useful. I am trying to develop a Java GUI within...
  • Ken: Hi Siddharth, There isn’t currently any way to move the docking controls. Feel free to submit an...
  • Phil: I have the same problem as described above with UITable working fine in the Matlab environment but showing no...
  • Siddharth: Is there any way to move the position of the docking controls (or eliminate them through some...
  • Chris: Yes, it is a challenge to organize functions into categories, especially with so many functions. Ken and I...
  • Mike: Thanks for fielding that one, Yair.
  • Yair Altman: Jimmy - if you mean that you wish to include hyperlinks in your function’s help comment, that will...
  • OysterEngineer: Thanks for explaining the Function Browser. I fired it up and gave it a try. It appears that it has...
  • Jimmy: Is there any way to include hyperlinks in a comment, such as the standard help at the beginning of a function?

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