isValidSymbol

Version 1.0.0.0 (7.41 KB) by Jan
Check for valid variable, field or function name - fast C-Mex
357 Downloads
Updated 4 Oct 2010

View License

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.

Cite As

Jan (2024). isValidSymbol (https://www.mathworks.com/matlabcentral/fileexchange/28936-isvalidsymbol), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Variables in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.0.0