{"id":2258,"date":"2008-07-10T13:06:17","date_gmt":"2008-07-10T18:06:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2008\/07\/10\/a-template-saves-you-time\/"},"modified":"2016-11-01T17:35:34","modified_gmt":"2016-11-01T21:35:34","slug":"a-template-saves-you-time","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2008\/07\/10\/a-template-saves-you-time\/","title":{"rendered":"A Template Saves You Time"},"content":{"rendered":"<div class=\"content\">\n<p>Jiro's pick this week is <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=6408&amp;objectType=file (link no longer works)\">NEWFCN<\/a> by Frank Gonz\u00e1lez-Morphy.<\/p>\n<p>We all have our own style of writing functions in MATLAB. I've adopted a convention that is similar to the \"best practice\"<br \/>\nthat The MathWorks uses for the shipping m-files.<\/p>\n<pre>     function out = myPlusFcn(varargin)\r\n     % MYPLUSFCN  Adds matrices\r\n     %   OUT = MYPLUSFCN(A, B) adds matrices A and B.\r\n     %   OUT = MYPLUSFCN(A, B, C, ...) adds all the matrices\r\n     %         provided. All of the matrices must be either\r\n     %         the same size or mix of same-sized matrices\r\n     %         and scalars.\r\n     %\r\n     % Examples:\r\n     %   out = myPlusFcn(1:3, 5);\r\n     %\r\n     %   out = myPlusFcn(2, eye(3), rand(3), magic(3));\r\n     %\r\n     % See also PLUS, SUM.<\/pre>\n<pre>        &lt;code starts here&gt;<\/pre>\n<div>\n<ul>\n<li>There is the <b>function definition line<\/b>.<\/li>\n<li>The first comment line is the <b>H1 line<\/b> used by commands like <tt>lookfor<\/tt>.<\/li>\n<li>The rest of the comment block is the <b>help text<\/b>.<\/li>\n<li>I try to include a few <b>examples<\/b> of calling the function.<\/li>\n<li><b>\"See also\"<\/b> entries help guide users to similar functions.<\/li>\n<\/ul>\n<\/div>\n<p>You can read about them <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/add-help-for-your-program.html\">here<\/a>.<\/p>\n<p>If I always create my functions this way, why not have a template for this. This is where <tt>NEWFCN<\/tt> by Frank comes into play. With this, you can have a head start on creating new functions. He has it customized for his own<br \/>\nstyle, so when you run<\/p>\n<pre>newfcn('myfcn')<\/pre>\n<p>it generates this m-file and opens it up in the editor:<\/p>\n<pre>     function myfcn()\r\n     % MYFCN ...\r\n     %\r\n     %   ...<\/pre>\n<pre>     %% AUTHOR    : Frank Gonzalez-Morphy\r\n     %% $DATE     : 17-Jun-2008 15:53:25 $\r\n     %% $Revision : 1.00 $\r\n     %% DEVELOPED : 7.6.0.324 (R2008a)\r\n     %% FILENAME  : myfcn.m<\/pre>\n<pre>     disp('!!!You must enter code into this file &lt;myfcn.m&gt;!!!')<\/pre>\n<pre>     % Created with NEWFCN.m by Frank Gonz\u00e1lez-Morphy\r\n     % Contact...: frank.gonzalez-morphy@mathworks.de\r\n     % ===== EOF ====== [myfcn.m] ======<\/pre>\n<p>What's nice about this is that some of the text are dynamically created, such as the file name, date, and the version. For<br \/>\nthose of you with different coding styles, his function is easy to understand so it shouldn't take much time to modify it.<\/p>\n<p>As an extra nugget of knowledge, you can also create a new function m-file with a basic template directly from the Current<br \/>\nDirectory Browser, but it's not as comprehensive as Frank's.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/createingnewfunctions1.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<p><b>Comments<\/b><\/p>\n<p>I think laziness fuels innovation. And I mean this in a good way! Thanks for writing this, Frank. How do you use MATLAB to<br \/>\nreduce your day-to-day work load? Tell us <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2258#respond\">here<\/a>.<\/p>\n<p><script>\/\/ <![CDATA[\nfunction grabCode_a185539c132f4c86935704a9016fa4fb() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='a185539c132f4c86935704a9016fa4fb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a185539c132f4c86935704a9016fa4fb';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Jiro Doke';\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\n\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }\n\/\/ ]]><\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\n<a><span style=\"font-size: x-small; font-style: italic;\">Get<br \/>\nthe MATLAB code<br \/>\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>Published with MATLAB\u00ae 7.6<\/p>\n<\/div>\n<p><!--\na185539c132f4c86935704a9016fa4fb ##### SOURCE BEGIN #####\n%%\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectId=1094142&objectType=author % Jiro>'s pick this week is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=6408&objectType=file % NEWFCN> by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectType=author&objectId=1094304 % Frank Gonz\u00c3\u00a1lez-Morphy>.\n%\n% We all have our own style of writing functions in MATLAB. I've adopted a\n% convention that is similar to the \"best practice\" that The MathWorks uses\n% for the shipping m-files.\n%\n%       function out = myPlusFcn(varargin)\n%       % MYPLUSFCN  Adds matrices\n%       %   OUT = MYPLUSFCN(A, B) adds matrices A and B.\n%       %   OUT = MYPLUSFCN(A, B, C, ...) adds all the matrices\n%       %         provided. All of the matrices must be either\n%       %         the same size or mix of same-sized matrices\n%       %         and scalars.\n%       %\n%       % Examples:\n%       %   out = myPlusFcn(1:3, 5);\n%       %\n%       %   out = myPlusFcn(2, eye(3), rand(3), magic(3));\n%       %\n%       % See also PLUS, SUM.\n%\n%          <code starts here>\n%\n% * There is the *function definition line*.\n% * The first comment line is the *H1 line* used by commands like |lookfor|.\n% * The rest of the comment block is the *help text*.\n% * I try to include a few *examples* of calling the function.\n% * *\"See also\"* entries help guide users to similar functions.\n%\n% You can read about them\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/f7-41453.html#f7-38065 % here>.\n%\n% If I always create my functions this way, why not have a template for\n% this. This is where |NEWFCN| by Frank comes into play. With this, you can\n% have a head start on creating new functions. He has it customized for his\n% own style, so when you run\n%\n%  newfcn('myfcn')\n%\n% it generates this m-file and opens it up in the editor:\n%\n%       function myfcn()\n%       % MYFCN ...\n%       %\n%       %   ...\n%\n%       %% AUTHOR    : Frank Gonzalez-Morphy\n%       %% $DATE     : 17-Jun-2008 15:53:25 $\n%       %% $Revision : 1.00 $\n%       %% DEVELOPED : 7.6.0.324 (R2008a)\n%       %% FILENAME  : myfcn.m\n%\n%       disp('!!!You must enter code into this file <myfcn.m>!!!')\n%\n%       % Created with NEWFCN.m by Frank Gonz\u00c3\u00a1lez-Morphy\n%       % Contact...: frank.gonzalez-morphy@mathworks.de\n%       % ===== EOF ====== [myfcn.m] ======\n%\n% What's nice about this is that some of the text are dynamically created,\n% such as the file name, date, and the version. For those of you with\n% different coding styles, his function is easy to understand so it\n% shouldn't take much time to modify it.\n%\n% As an extra nugget of knowledge, you can also create a new function\n% m-file with a basic template directly from the Current Directory Browser,\n% but it's not as comprehensive as Frank's.\n%\n% <<createingnewfunctions1.png>>\n%\n% *Comments*\n%\n% I think laziness fuels innovation. And I mean this in a good way! Thanks\n% for writing this, Frank. How do you use MATLAB to reduce your day-to-day\n% work load? Tell us <https:\/\/blogs.mathworks.com\/pick\/?p=2258#respond % here>.\n\n##### SOURCE END ##### a185539c132f4c86935704a9016fa4fb\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\nJiro's pick this week is NEWFCN by Frank Gonz\u00e1lez-Morphy.<br \/>\nWe all have our own style of writing functions in MATLAB. I've adopted a convention that is similar to the \"best practice\"<br \/>\nthat The... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/07\/10\/a-template-saves-you-time\/\">read more >><\/a><\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2258"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2258"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2258\/revisions"}],"predecessor-version":[{"id":8001,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2258\/revisions\/8001"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}