What letters can be used in variable names?

54 Ansichten (letzte 30 Tage)
David Liebtag
David Liebtag am 9 Jan. 2013
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

Antworten (3)

Walter Roberson
Walter Roberson am 9 Jan. 2013
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 Kommentare
Daniel Shub
Daniel Shub am 9 Jan. 2013
@Jan If È is not a letter why does isletter('È') say it is?
Jan
Jan am 9 Jan. 2013
@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.

Melden Sie sich an, um zu kommentieren.


Daniel Shub
Daniel Shub am 9 Jan. 2013
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 Kommentare
David Liebtag
David Liebtag am 9 Jan. 2013
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
Daniel Shub
Daniel Shub am 9 Jan. 2013
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.

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 9 Jan. 2013
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.

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by