tags:

views:

90

answers:

4

In MATLAB, when you click File -> New -> Function M-File, you get a file with the following contents:

function [ output_args ] = Untitled( input_args )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here


end

Is it possible to override this behaviour, and specify your own text?

(The motivation is that I'm trying to persuade my colleagues to document their m-files more thoroughly, and having default text for them to fill in might encourage them.)

+4  A: 

I didn't even know File->New->Function did that.

The way I solved the issue was to write a function that you call via

>>newFunction myNewFunctionName

It then

  1. pops up an inputdlg window, which asks the user for the synopsis and the H1 line and allows to already write help to explain input and output arguments. There, the user also selects whether myNewFunctionName is a function or a class in order to choose the right header and 'function call'
  2. checks whether a function of the same name exists already
  3. asks for a folder to save the function, and
  4. opens the function in the editor

The header is set up so that it's easy to fill in info about input and output. It also automatically lists the username of the person who created the file as well as the date and the Matlab version.

EDIT For new classes, the template function automatically makes sure that they subclass my general superclass that implements methods such as 'help' (which calls doc(class(obj)) )

Now if the template functionwould also write the algorithm part of the function, it would be really convenient. :)

EDIT2 Here's a link to the function on the file exchange.

Jonas
Thanks, that's quite a novel solution. Maybe a function-writer GUI could implement this nicely. I feel it's important not to underestimate the laziness/hatred of documentation factor though. It still seems more effort than clicking File->New->Function-with-stuff-written-for-me.
Richie Cotton
@Richie Cotton: the function is called as 'codeTemplate myNewFunction' and then asks the user for the additional input via inputdlg. I require the H1-line so that there's some minimum documentation
Jonas
@Richie Cotton: I updated my answer a bit. I don't think this is anymore involved than clicking File->New->Function, though you could skip the step where the dialog window pops up. However, other than requiring the H1 line, it is actually less work than the Matlab default, since you don't need to replace 'untitled' with 'myFunctionName' everywhere.
Jonas
@Jonas: Have you considered to put your function on File Exchange?
yuk
@yuk: not till now. I will add a link to it once it has passed review
Jonas
@Jonas: I'm opting for a stripped down version of this. I like the idea of a function autogenerating content. However, since you don't always want to write the documentation immediately when you create the file, asking all these questions might be too instrusive. I'll post my function to the file exchange once I've road-tested it.
Richie Cotton
Another good idea I've just had is to create a shortcut that calls the function, so the file can be opened with a single click. As I said, never underestimate laziness ...
Richie Cotton
@Richie Cotton: This is an excellent idea! As you'll see from my function, adding help for input and output is optional, but the H1 line is something you should be able to write for every function right away. After all, when you start a new function, you should have an idea what it does.
Jonas
@yuk: There you go.
Jonas
+2  A: 

I would suggest making your own default m-file template, called default.m for example, and placing it in a folder on the MATLAB path where your colleagues can access it. You should then set the file to be read-only. Your colleagues can then execute any one of the following commands in the MATLAB Command Window when they want to create a new function m-file:

open default.m
open('default.m')
edit default.m
edit('default.m')

The functions OPEN and EDIT will open a file in the MATLAB Editor. Since the file default.m is read-only, if anyone tries to save over it they will get a dialog box warning them as such and asking them to save to a new file (or overwrite it). That should keep them from accidentally modifying the template.

gnovice
@gnovice: I like that this isn't intrusive, and that it's easy to do. It does have the disadvantage of requiring the user to manually fill in everything though.
Richie Cotton
+1  A: 

I searched through all text files starting from matlabroot folder, but could not find that template. Seems it's hard-coded, which is weird.

I like Jonas approach. As my two cents, you can download a function (not mine) doing similar things with some customization from here.

yuk
@yuk: That second link gets blocked by my internet filter, claiming to be malware; are you sure it is correct?
Richie Cotton
@Richie: Opened ok from my work and home. It's just a Matlab blog: http://msbs.ca/matlab/
yuk
@yuk: Must just be an overzealous filter at work; it opens fine at home. The use of `com.mathworks.mlservices.MLEditorServices.newDocument(str)` to put the template directly into the editor is interesting.
Richie Cotton
A: 

After more pondering, I've come up with a solution that I'm happy with, combining Jonas' and gnovice's answers. It's a function that creates a new m-file (with template documentation), and opens it in the editor. It is available from the Matlab Central File Exchange.

Richie Cotton

related questions