What letters can be used in variable names?
51 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(3 个)
Walter Roberson
2013-1-9
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 个评论
Jan
2013-1-9
@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.
Daniel Shub
2013-1-9
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 个评论
Daniel Shub
2013-1-9
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.
Jan
2013-1-9
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!