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.

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 ) grabs:- either a space followed by output arguments and the equal sign
- or a space alone (when there are no output arguments)

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

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

file | file size | comments |
---|---|---|

A2pdf.sh | 545 B | none |

A2ps.sh | 445 B | none |

XEmacs_quickref.pdf | 80 KB | none |

auctex-ref.pdf | 96.25 KB | none |

createIEEEPDF.sh | 622 B | none |

createWinVid.sh | 800 B | none |

ctags.zip | 241 B | featured by this article |

vpnclient-linux-2.6.19+-rev1.diff | 3.54 KB | none |