Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 976
Richie Cotton Avatar asked Mar 31 '10 16:03

Richie Cotton


2 Answers

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.

like image 114
Jonas Avatar answered Sep 29 '22 08:09

Jonas


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.

like image 23
gnovice Avatar answered Sep 29 '22 08:09

gnovice