What letters can be used in variable names?

조회 수: 55 (최근 30일)
David Liebtag
David Liebtag 2013년 1월 9일
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

답변 (3개)

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

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by