File Exchange Pick of the Week

July 10th, 2008

A Template Saves You Time

Jiro's pick this week is NEWFCN by Frank González-Morphy.

We all have our own style of writing functions in MATLAB. I've adopted a convention that is similar to the "best practice" that The MathWorks uses for the shipping m-files.

     function out = myPlusFcn(varargin)
     % MYPLUSFCN  Adds matrices
     %   OUT = MYPLUSFCN(A, B) adds matrices A and B.
     %   OUT = MYPLUSFCN(A, B, C, ...) adds all the matrices
     %         provided. All of the matrices must be either
     %         the same size or mix of same-sized matrices
     %         and scalars.
     %
     % Examples:
     %   out = myPlusFcn(1:3, 5);
     %
     %   out = myPlusFcn(2, eye(3), rand(3), magic(3));
     %
     % See also PLUS, SUM.
        <code starts here>
  • There is the function definition line.
  • The first comment line is the H1 line used by commands like lookfor.
  • The rest of the comment block is the help text.
  • I try to include a few examples of calling the function.
  • "See also" entries help guide users to similar functions.

You can read about them here.

If I always create my functions this way, why not have a template for this. This is where NEWFCN by Frank comes into play. With this, you can have a head start on creating new functions. He has it customized for his own style, so when you run

newfcn('myfcn')

it generates this m-file and opens it up in the editor:

     function myfcn()
     % MYFCN ...
     %
     %   ...
     %% AUTHOR    : Frank Gonzalez-Morphy
     %% $DATE     : 17-Jun-2008 15:53:25 $
     %% $Revision : 1.00 $
     %% DEVELOPED : 7.6.0.324 (R2008a)
     %% FILENAME  : myfcn.m
     disp('!!!You must enter code into this file <myfcn.m>!!!')
     % Created with NEWFCN.m by Frank González-Morphy
     % Contact...: frank.gonzalez-morphy@mathworks.de
     % ===== EOF ====== [myfcn.m] ======

What's nice about this is that some of the text are dynamically created, such as the file name, date, and the version. For those of you with different coding styles, his function is easy to understand so it shouldn't take much time to modify it.

As an extra nugget of knowledge, you can also create a new function m-file with a basic template directly from the Current Directory Browser, but it's not as comprehensive as Frank's.

Comments

I think laziness fuels innovation. And I mean this in a good way! Thanks for writing this, Frank. How do you use MATLAB to reduce your day-to-day work load? Tell us here.


Get the MATLAB code

Published with MATLAB® 7.6

One Response to “A Template Saves You Time”

  1. Quan Quach replied on :

    I recently used MATLAB to help me correct a capitalization mistake throughout my first 40 or so posts at blinkdagger. MATLAB can solve all problems it seems.

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).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • oleg: The author has implemented skewness, kurtosis and checks answering appropriately to the critic.
  • Ashok: how to store 10 or more random number in a loop a loop for i = 1:n mean(i) = input(’enter the mean value...
  • Ben: Doug, Thanks for the very helpful videos! Uitables seem like a convenient way to make a customized property...
  • oleg: Allstats has no checks, no comments and could also be improved (talking about prctile implementatio). Not to...
  • Todd: Additional information and a link to download free MATLAB and Simulink LEGO MINDSTORMS NXT code can be found at...
  • Doug: @Leo, Here is the “English version” of that code. “vec = []” makes an empty variable...
  • leo: Dear Doug I have a question in your code ‘October 9th, 2009 at 13:53′ vec = []; vec = [vec val]; I...
  • Shanker Keshavdas: You sir, are a gentleman and a scholar… No really, helped me out a lot. As to what is...
  • Quan Zheng: how can I get a copy of stepspecs.m?
  • Doug: @Lucy I think this is what you seek to move a line with the mouse in MATLAB. http://blogs.math...

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