Why is 'Character#' not a valid mathlab variable name?
Show older comments
Why is 'Character#' not a valid MATLAB variable name?
Answers (3)
DGM
on 17 Feb 2023
0 votes
"A valid variable name starts with a letter, followed by letters, digits, or underscores."
2 Comments
Walter Roberson
on 17 Feb 2023
But that doesn't explain why ... ;-)
DGM
on 18 Feb 2023
According to inside sources, it's because a very tall hamster wanted it that way.
Walter Roberson
on 17 Feb 2023
Edited: Walter Roberson
on 18 Feb 2023
0 votes
I looked up several different lists of the most popular programming languages, and checked reference documentation for each .
# is not a valid character in variable names in:
- Ada 95 (but might be possible in Ada 2003)
- Algol
- C
- C++
- C#
- fortran
- Go
- HCL (permits $ but not as first character)
- java
- javascript
- Kotlin
- Lua
- Makefile (‘:’, ‘#’, ‘=’, and whitespace are specifically prohibitted in variable names)
- pascal
- php
- python
- R (permits periods in identifiers)
- Ruby
- Shell
- Swift (permits $ and a number of Unicode characters in variable names, but not # )
- typescript (permits $ including as first character)
Ones that do permit #:
- Ada 2003 (permits many unicode characters)
- Common Lisp (requires special syntax) (permits many unicode characters)
- Maple (requires special syntax) (permits many unicode characters)
- Mathematica (permits many unicode characters)
- Rust (permits many unicode characters)
it is difficult to answer exactly "why" any of these languages made the choices they did.
I suspect in the case of MATLAB it is because the permitted identifiers are the same as the identifier forms permitted by the linkers on the operating systems of implementation. (In some cases the $ is also permitted by the linkers, but it tends to have special meaning when it is permitted.)
John D'Errico
on 18 Feb 2023
Edited: John D'Errico
on 19 Feb 2023
MATLAB itself can tell you. Just ask.
why
why
why
why
why
Do you want a better reason? Just because. Seriously, does there need to be a better reason? Sometimes you just need to accept something as it is.
You should understand that special characters in names often may create subtle problems. For example, why would MATLAB not allow you to start a variable name with a number? That is, why can x5 be a variable name, but not 5x? This may get into parser issues, where by removing the possibility of a number at the beginning of the name, it made the parser code far easier to write. Eliminating that possibility reduces the chances for bugs in your own code too.
But special characters often have specific uses. For example, the @ symbol has specific uses in MATLAB. They could not allow you to use it. As well symbols like ~ also have their own uses. And who knows when next year, they might find a real need for some other special character as a new operator of some sort? This means the language would need to exclude certain specific special characters from use in variable names. So instead of giving the user a list of which special chacracters you could use, it is far easier and better to just declare ALL special characters as illegal in a variable name. That makes the rules for a variable name quite simple. And in turn, that is a good thing.
By the way, it is MATLAB, not mathlab.
1 Comment
Steven has a comment that I think is a good addition:
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!