File Exchange

image thumbnail

isValidSymbol

version 1.0 (7.41 KB) by

Check for valid variable, field or function name - fast C-Mex

0 Downloads

Updated

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.

Comments and Ratings (0)

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video