Ctags for Matlab files

Here I will show how to create a tags file for matlab files with the ctags program.

PLEASE NOTE: This article is only useful for versions of ctags below/or equal to 5.7 (check via ctags --version). In the recent versions (5.8 and above) a matlab support has been added to the standard languages set of ctags, beside other bugfixes.

The solution is quite simple: Create a .ctags file in your home directory (or change it when it already exists) and add the following lines:

--langdef=matlab
--langmap=matlab:.m
--regex-matlab=/^function.*=*[ \t]([,a-zA-Z0-9_]+)/\1/f,function/

Now switch to your matlab code (with probably existing subdirectories) and execute the following command:

bash:~/yourCodeDir$ ctags -R .

A "tags" file should appear containing the function definitions of your code.

Update

With great help of Stephen Wilkinson we improved the regular expression above to the following form:

--regex-matlab=/^function([] A-Za-z0-9,_[]+=[ ]?|[ ]+)([^.(]+).*$/\2/f,function/

Steve wrote:

The difference is that this regular expression can handle output arguments of Matlab functions. I have tested it with the following function prototypes which are all valid in Matlab:

function funname
function funname()
function funname(a,b,c)
function funname(a, b, c)
function ab = funname(cc)
function [ab] = funname (a,b, c)
function [out1, out2] = funname(in1, in2)

and in each case it returns funname.

The regular expression uses the | (or) operator. The first captured subexpression (which could be referenced with \1) grabs:

The second captured subexpression (which is referenced with \2) grabs the function name, i.e. anything up to a possible open bracket ("

However, this regular expression still has one major drawback: Functions defined over several lines with the '...' statement where 'funname' doesn't appear in the first line are excluded from the tags file. Here is an example:

function [a,b,c,d,e] = ...
    funname (f,g,h)

According to the ctags homepage this problem can't be solved with regular expressions. It has to be written a parser for that. Well...

Update II

An example ctags configuration file is now uploaded to the downloads section below. Saves copy&pase time ;-)

Section Blog: Available Downloads

 
filefile sizecomments
A2pdf.sh545 Bnone
A2ps.sh445 Bnone
XEmacs_quickref.pdf80 KBnone
auctex-ref.pdf96.25 KBnone
createIEEEPDF.sh622 Bnone
createWinVid.sh800 Bnone
ctags.zip241 Bfeatured by this article
vpnclient-linux-2.6.19+-rev1.diff3.54 KBnone
Author: Christoph Hermes, published: 2008-04-16 14:19:27