Richie Cotton
Richie Cotton

Reputation: 121127

How to override the default text in MATLAB

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

Upvotes: 6

Views: 1877

Answers (4)

Richie Cotton
Richie Cotton

Reputation: 121127

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.

Upvotes: 0

Jonas
Jonas

Reputation: 74940

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.

Upvotes: 6

yuk
yuk

Reputation: 19880

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.

Upvotes: 1

gnovice
gnovice

Reputation: 125874

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.

Upvotes: 2

Related Questions