tags:

views:

389

answers:

3

I have a set of objects that I read information out of that contain information that ends up becoming a MATLAB m file. One piece of information ends up being a function name in MATLAB. I need to remove all of the not-allowed characters from that string before writing the M file out to the filesystem. Can someone tell me what characters make up the set of allowed characters in a function name for MATLAB?

+1  A: 

Edited:

this may be more informative:

http://scv.bu.edu/documentation/tutorials/MATLAB/functions.html

ChristopheD
There are more rules than I realized.
Jay R.
+4  A: 

The short answer...

Any alphanumeric characters or underscores, as long as the name starts with a letter.

The longer answer...

The MATLAB documentation has a section "Working with M-Files" that discusses naming with a little more detail. Specifically, it points out the functions NAMELENGTHMAX (the maximum number of characters in the name that the OS will pay attention to), ISVARNAME (to check if the variable/function name is valid), and ISKEYWORD (to display restricted keywords).

gnovice
+5  A: 

Legal names follow the pattern [A-Za-z][A-Za-z0-9_]*, i.e. an alphabetic character followed by zero or more alphanumeric-or-underscore characters, up to NAMELENGTHMAX characters.

Since MATLAB variable and function naming rules are the same, you might find genvarname useful. It sanitizes arbitrary strings into legal MATLAB names.

SCFrench

related questions