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.

  • Beats: Not useful for everyday but very interesting. Thanks.
  • nickey: Hi Brett, I’ve tested Tao’s program and yours too. They ran successfully. I’ve tried to...
  • Kika: THANK YOU!! I’ve been trying to change the colormap but I didn’t know what to do. It was driving me crazy!...
  • Richard: Hello Doug, yet another question. I followed this tutorial: http://blinkdagger.c om/matlab/matlab-...
  • Doug: Richard, Use the compiler options file, it is found at the bottom of the dialog that you get when clicking on...
  • Richard: To whom it may concern, a solution to the dos screen annoyance is found by user ‘vihang’ in this...
  • Richard: Thank you for your reply. I understand that I can specify flags using the mcc command, but is this also...
  • Doug: Richard: Find it here: http://www.mathworks .com/access/helpdesk /help/toolbox/compil er/mcc.html Doug
  • Richard: Hello Doug, Can you explain point 56. (about the -e flag) because I can’t seem to find it. Regards,...
  • Doug: Tony, When you say the .MAT file has 984 files, you mean it has the *names* of the files. So the real problem...

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