special character in symbolic variable name

31 views (last 30 days)
I have discovered a nice way to name symbolic variables and would like to extend it a bit, but can't find the correct documentation. First, I created a MuPAD notebook, and used the statement
xi0:=Symbol::subScript(Symbol::xi,0):
Then, I converted the notebook to a Live Script, and saw the following code
syms xi_0
xi0=xi_0;
With that, I can create expressions using xi0 and they are displayed very nicely in the Live Script output. If possible, I would like to extend this and create a variable for the absolute value of 'q', and tried the following (based on the Unicode character for a double vertical line):
doubleVerticalLine=char(8214)
qabs=strcat(doubleVerticalLine,'q',doubleVerticalLine)
eval(['syms ',qabs]);
Here are my problems:
  1. I can't find the documentation for syms xi_0. This is not Tex, and there is no "\" in front of the xi, so I don't know how to extend it. I do know that the underline means subscript in MATLAB.
  2. I haven't found a way to make this work using eval, not even for the simple case of xi.
  3. It looks like the double vertical line is not allowed in variable names, so I would need to find something else, but where is the documentation for it? Maybe I could somehow convert on output, not in the expression itself.
I would be happy to share my script individually, but I don't want to broadcast this very unfinished piece of work to a large audience.

Answers (1)

Steven Lord
Steven Lord on 6 Oct 2016
You are correct that the double vertical line is not an allowed character in a variable name in MATLAB, as it doesn't satisfy the requirements given in the documentation for the isvarname function.
In general creating dynamic variable names is strongly discouraged, though your case is slightly different than the usual.
  2 Comments
Thomas Ligon
Thomas Ligon on 6 Oct 2016
Hello Steven,
thanks for the comments!
I can see that I can use isvarname to check the validity of a potential name, but I can also just read the error message when the string is not acceptable, with the same result. When I look at the documentation of isvarname, it points me to Variable Names, which explains that the name must begin with a letter. Fine, but is a Greek letter a letter? And why can I just use alpha or xi without any escape characters?
You also explained that dynamic variable names are strongly discouraged, but are these names dynamic? I'm just looking for a way to use Greek letters and maybe other special characters.
By the way, the following code works nicely in a MuPAD notebook:
qabs:=_concat(Symbol::Verbar,"q",Symbol::Verbar);
qabs;
qabs^2;
But it gets converted to the following Live Script code:
syms Verbar
qabs = sym(matlab.lang.makeValidName([char(Verbar), char('q'), char(Verbar)]))
qabs
qabs^2
so this is a small feature of MuPAD notebooks that cannot be converted to Live Scripts.
Walter Roberson
Walter Roberson on 6 Oct 2016
In MATLAB, variable numbers must begin with a "letter", after which they can have "letter" or the digits 0 to 9, or underscore. The permitted letters are A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z .
To put it another way, the letters must satisfy
TheCharacter <= 127 & isstroprop(TheCharacter, 'alpha')
where TheCharacter is the 16 bit Unicode encoding of the character (characters beyond U+FFFF are not supported in MATLAB.).
The first 127 Unicode positions are the first 127 ISO-8996-1 positions, which are in turn the first 127 US-ASCII positions.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!