How can I convert a char vector according to the readtable VariableNamingRule

1 vue (au cours des 30 derniers jours)
Frank Hofmann
Frank Hofmann le 10 Déc 2021
Commenté : Rik le 10 Déc 2021
I would like to convert a character vector according to the VariableNamingRule = 'modify' algorithm of the readtable function.
Example:
Original vector: '123 Character?Vector_With*Special Characters_'
Converted vector: 'x123Character_Vector_With_SpecialCharacters_'
Is there a standard MATLAB function which does this conversion (preferentially without regexp)?
  1 commentaire
Stephen23
Stephen23 le 10 Déc 2021
You could use GENVARNAME, but it does not always return the prettiest text:
genvarname('123 Character?Vector_With*Special Characters_')
ans = 'x123Character0x3FVector_With0x2ASpecialCharacters_'

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 10 Déc 2021
Modifié(e) : Rik le 10 Déc 2021
Yes, there is:
S='123 Character?Vector_With*Special Characters_';
N = matlab.lang.makeValidName(S)
N = 'x123Character_Vector_With_SpecialCharacters_'
If you want to make sure to have the same behavior for any Matlab release, you will have to use a slightly different strategy. Feel free to have a look at my JSON-parser. It is neither fancy nor fast, but it has a stable behavior when generating the variable name. I'm not sure it exactly matches the readtable behavior, or genvarname.
  2 commentaires
Rik
Rik le 10 Déc 2021
You're welcome. If my answer solved your question, please consider marking it as accepted solution, otherwise, feel free to comment with any follow-up questions.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by