What letters can be used in variable names?

The MATLAB documentation's discussion of variable names says, "A valid variable name starts with a letter, followed by letters, digits, or underscores."
Can anyone tell me the list of valid letters?
(I'm writing a program which prompts users for new variable names and I would like to correctly validate the names before passing them to MATLAB.)
Thank you.
David Liebtag

Answers (3)

The letters allowed in variable names are:
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
The allowed digits are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
The other allowed characters are:
_ (underscore)
As you found, the first character must be a letter.
You might be interested in the call genvarname()

4 Comments

So just to be sure I understand, MATLAB does not support use of alphabetic characters from national languages other than English in variable names. For example, these characters are invalid:
ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝÞß àáâãäåæçèéêëìíîïðñòóôõöùúûüýþÿ
Correct?
Also, can you direct me to a page in the documentation that states these rules?
Thank you.
David Liebtag
Jan
Jan on 9 Jan 2013
Edited: Jan on 9 Jan 2013
Yes, this is correct. The statement, that you can only use '...e...E...' means, that you cannot use '...ÈÉÊË...èéêë...'.
You can find the page in the documentation, when you ask your favorite serach engine for "Matlab valid varaible name": http://www.mathworks.com/help/matlab/matlab_prog/variable-names.html
It is surprising, that you ask for the documentation, although you have cited it already in your question.
The same rules apply for function names and field names, but for the latter case, keywords are allowed (see help iskeyword).
@Jan If È is not a letter why does isletter('È') say it is?
@Daniel: Obviously the term "letter" has different meanings in the isletter function and in the documentation of valid variable names. I'm convinced that you are aware of this. Therefore I guess that your question has the intention to ask me to send an enhancement request to TMW concerning the link I've posted. I admit that this is a bold guess, but I have done it already.

Sign in to comment.

I think the function you are looking for is called isvarname. This handles the special keyword strings (e.g., "end" and "for") which are included in iskeyword as well as the start with letter and only include letter, numbers and underscore requirements. While MATLAB provides this function, use it at your own risk. Just because a variable name is valid, doesn't mean it is a good idea to use. For example if the user requests a variable with the same name as a function (or another variable) in you function, havoc will ensue. It would be safer to add the variables as fields of a structure (hence protecting the name space), but this may cause problems later.

2 Comments

Thank you, but I am not looking for a way to validate variable names. Rather, I am looking for the exact list of valid letters which I should let the user type in an entry field.
David Liebtag
Ahh, sorry. I misinterpreted: "I would like to correctly validate the names before passing them to MATLAB." Now I understand that you are getting the string in a program that is not MATLAB.
The documentation is pretty thin. For example, the documentation of isvarname talks about "letters" and isletter('À') returns true, so clearly À is a letter. Of course, as far as isvarname is concerned it is not.
In my experience, Walter's list is correct. Just be aware that keywords are also not allowed and there is also a maxnamelength constraint.

Sign in to comment.

There is a built-in not documented function, which can be reached through a MEX function only: mxIsValidMatName.
I needed a test for thousands of names and this was too slow:
valid = false(1, length(name));
for ii = 1:length(name)
valid(ii) = strcmp(name{ii}, genvarname(name{ii}));
end
Using isvarname was not fast enough also. Therefore I've written FEX: isValidSymbol, which accepts cell strings also.

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Asked:

on 9 Jan 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!