How can I convert a char vector according to the readtable VariableNamingRule
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2021 年 12 月 10 日
You could use GENVARNAME, but it does not always return the prettiest text:
genvarname('123 Character?Vector_With*Special Characters_')
採用された回答
Rik
2021 年 12 月 10 日
編集済み: Rik
2021 年 12 月 10 日
Yes, there is:
S='123 Character?Vector_With*Special Characters_';
N = matlab.lang.makeValidName(S)
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 件のコメント
Rik
2021 年 12 月 10 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!