Effacer les filtres
Effacer les filtres

How can i use a symbol as a variable ?

16 vues (au cours des 30 derniers jours)
Mallouli Marwa
Mallouli Marwa le 7 Avr 2017
Commenté : Walter Roberson le 20 Fév 2023
I want to define a varibale alpha_ks as a symbol \alpha_ks but an error will be appeared.
function [a, x, ...]= MFC ()
  3 commentaires
KSSV le 7 Avr 2017
I don't think you can use symbol as a variable..What's the necessity too by the way?
dbmn le 7 Avr 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.

Connectez-vous pour commenter.

Réponses (1)

dpb le 7 Avr 2017
Modifié(e) : dpb le 7 Avr 2017
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 commentaires
dpb le 7 Avr 2017
Modifié(e) : dpb le 7 Avr 2017
I still think of it as those are same reasons the rules exist in C/C++ (as well as others); hence I think of it in those terms. So perhaps it isn't strictly owing to the underlying language itself but it's the same problem in the compiler they're solving.
Don't see anything fundamentally against increasing the scope of what the character set could be albeit again still need the proscription of starting with character that is a letter where 'letter' could also include at least subset of unicode characters.
It just has to be unique in that it doesn't have an interpretation otherwise; there's no reason the greek alphabet (say) in a character set that contains such couldn't just as well be included in the definition of "letter".
Walter Roberson
Walter Roberson le 20 Fév 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)

Connectez-vous pour commenter.


Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by