isValidSymbol
ISVALIDSYMBOL - Check string to be a valid Matlab symbol
This function is equivalent to Matlab's ISVARNAME, but accepts cell strings as input also. While the speed is comparable for strings, it is *much* faster for cell strings (see screenshot).
L = isValidSymbol(S)
INPUT:
S: String or cell string.
OUTPUT:
L: LOGICAL scalar, if S is a string. LOGICAL array of the same size
as S for a cell string.
L is TRUE for valid symbols:
The 1st character must be a letter, the following must be a letter,
number or the underscore. The length must not exceed 63 characters.
EXAMPLES:
isValidSymbol('a') % >> true
isValidSymbol('a1') % >> true
isValidSymbol('a_') % >> true
isValidSymbol({'_a', 'b'}) % >> [false, true]
isValidSymbol('1a') % >> false
s = repmat('a', 1, 63);
isValidSymbol(s) % >> true
isValidSymbol([s, 'a']) % >> false
AIM OF THIS SUBMISSION:
Actually the C subfunction "CheckCString" was developed to support the safe creation of structs and MAT files in C-Mex functions. You can easily include the test to your Mex-projects - just remove the main function "mexFunction".
For checking Matlab strings the conversion to a C-string wastes time. Therefore the subfunction "CheckMatlabString" operates on the mxChar-vector directly.
You can accelerate Matlab's slow GENVARNAME remarkably by calling this function instead of a loop over ISVARNAME calls.
COMPILATION: see isValidSymbol.c
PRE-COMPILED MEX: http://www.n-simon.de/mex
TEST: Run uTest_isValidsymbol to check validity and speed of the Mex function.
Tested: Matlab 6.5, 7.7, 7.8, BCC5.5, LCC2.4/3.8, MSVC 2008, WinXP 32 bit
See also ValidSymbol, NAMELENGTHMAX, ISKEYWORD, ISVARNAME, GENVARNAME.
Cita come
Jan (2025). isValidSymbol (https://it.mathworks.com/matlabcentral/fileexchange/28936-isvalidsymbol), MATLAB Central File Exchange. Recuperato .
Compatibilità della release di MATLAB
Compatibilità della piattaforma
Windows macOS LinuxCategorie
- MATLAB > Programming > Functions > Variables >
Tag
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Scopri Live Editor
Crea script con codice, output e testo formattato in un unico documento eseguibile.
| Versione | Pubblicato | Note della release | |
|---|---|---|---|
| 1.0.0.0 |
