Code covered by the BSD License  

Highlights from
isValidSymbol

Be the first to rate this file! 4 Downloads (last 30 days) File Size: 7.41 KB File ID: #28936
image thumbnail

isValidSymbol

by

 

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

| Watch this File

File Information
Description

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.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.

Contact us