{"id":278,"date":"2008-12-01T18:59:37","date_gmt":"2008-12-01T18:59:37","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2008\/12\/01\/publishing-functions\/"},"modified":"2018-02-12T14:32:56","modified_gmt":"2018-02-12T19:32:56","slug":"publishing-functions","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2008\/12\/01\/publishing-functions\/","title":{"rendered":"Publishing Functions"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/fitdemo.thumbnail.png\" alt=\"fitdemo.png\" align=\"left\" \/>Until MATLAB R2008a, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/publish.html\"><tt>PUBLISH<\/tt><\/a> only worked on scripts.\u00a0 One of the limitations of the MATLAB language is that you cannot define a function from within a script.\u00a0 These two worked against each other to make it difficult to tell a story with M-file publishing that relied on another function.<\/p>\n<p>A good workaround to this problem is to use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/type.html\"><tt>TYPE<\/tt><\/a> or <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/dbtype.html\"><tt>DBTYPE<\/tt><\/a> to display the secondary function. An example of this is one of the demos included in MATLAB, <a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/fitdemo.html\">Optimal Fit of a Non-linear Function, or FITDEMO<\/a>.\u00a0 Because <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/publish.html\">FMINSEARCH<\/a> requires a function to optimize, <tt>FITFUN<\/tt> needs to be defined in a separate file and included into the narrative with <tt>TYPE<\/tt>.<\/p>\n<p>This works fairly well, but there are a couple disadvantages to this technique.\u00a0 Because <tt>FITFUN<\/tt> is displayed as the output from <tt>TYPE<\/tt>, it is just gray text without syntax highlighting.\u00a0 You also now have two source M-files to manage, so functions like <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2008b\/techdoc\/ref\/grabcode.html\"><tt>GRABCODE<\/tt><\/a> won&#8217;t work as well.<\/p>\n<p>In MATLAB R2008a, we made it possible for <tt>PUBLISH<\/tt> to handle not just scripts, but functions too.\u00a0 Converting <tt>FITDEMO<\/tt> into a function allows us to include <tt>FITFUN<\/tt> in it as a nested function.\u00a0 <a href=\"https:\/\/blogs.mathworks.com\/images\/desktop\/fitdemonew.html\">The new version<\/a> fixes both of the disadvantages mentioned above and allows greater flexibility in your M-file storytelling.<\/p>\n<p><em>Matthew Simoneau is a developer at The MathWorks focused on technical communication and social computing.\u00a0 In addition to <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/2924\">occasional contributions<\/a> to MATLAB Central, he blogs sporadically on his <a href=\"http:\/\/www.matthewsim.com\/\">personal site<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Until MATLAB R2008a, PUBLISH only worked on scripts.\u00a0 One of the limitations of the MATLAB language is that you cannot define a function from within a script.\u00a0 These two worked against each other to&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2008\/12\/01\/publishing-functions\/\">read more >><\/a><\/p>\n","protected":false},"author":67,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9,18],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/278"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/278\/revisions"}],"predecessor-version":[{"id":5288,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/278\/revisions\/5288"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}