How can i use a symbol as a variable ?

38 views (last 30 days)
Mallouli Marwa
Mallouli Marwa on 7 Apr 2017
Commented: Walter Roberson on 20 Feb 2023
Hi
I want to define a varibale alpha_ks as a symbol \alpha_ks but an error will be appeared.
function [a, x, ...]= MFC ()
a='\alpha_ks';
x=5;
end
  3 Comments
dbmn
dbmn on 7 Apr 2017
If you want to display nice formulas, you could do so in the new live scripts.
In the (text based) code it doesn't make sense.

Sign in to comment.

Answers (1)

dpb
dpb on 7 Apr 2017
Edited: dpb on 7 Apr 2017
From
help isvarname
... valid variable name is a character string of letters, digits and
underscores, with ... the first character a letter, ...
While an (extended) character, '\alpha_ks' is not in the set of letters, digits, underscores. Ergo, it can't be a variable name by itself nor even as part of a longer name within which it occurs. "Sometimes we just can't have everything we want."
Another comment mentioned it having to do with text-based code which isn't really the issue; the restriction is that Matlab is based on the C/C++ coding language internally and these are the same rules for valid variable names in C. Nothing says TMW couldn't have expanded the allowable character set inside the Matlab interpreter, but would add another parsing overhead step as any variable name outside the naming rules would have to have another internal name generated for it that would follow the internal coding rules; potentially another real speed bottleneck when also would need to translate that back for display. Much easier to just follow the rules as they exist despite the lack of being able to write code that looks as much like the algebra of the underlying expression as might like. I suspect these kinds of restrictions will loosen significantly over time, but "not yet".
  3 Comments
Walter Roberson
Walter Roberson on 20 Feb 2023
Back in 2017, most versions of MATLAB did not use UTF8 encoding for the file, and so only 8 bit codes that did not use the UTF control sequences could be reliably used... and even then only if there was no conflicting 8 bit code. For this purpose a conflicting code would be punctuation in one character set but letter-like in another character set: with 8 bit codes you would not be able to tell which is which. It does not matter if in one character set 0xC3 is 'Ă' but it is 'Ä' in a different character set: you could treat both as letters. It would matter if a character was Ϋ in one character set but … (ellipsis) in a different character set as those have different roles and … would probably be expected to act the same way as ... (three periods)

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!

Translated by