MATLAB Community

MATLAB, community & more

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.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.