How to replace 'genvarname' with 'matlab.la​ng.makeVal​idName'?

Hi,
I'm using 'genvarname' to create variables names from strings. Example:
temp = 'abc'
v = genvarname(temp)
eval([v,'=5'])
Result
abc = 5
In Matlab 2017a I've got a suggestion to replace 'genvarname' with 'matlab.lang.makeValidName' as 'getvarname' will be removed from future releases. Could anyone help me with rewriting my example using 'matlab.lang.makeValidName'?

1 件のコメント

Stephen23
Stephen23 2017 年 3 月 21 日
編集済み: Stephen23 2017 年 3 月 21 日
Creating or accessing variables names dynamically will make code buggy and slow, as a thousand discussions and threads on this forum have explained:
The documentation advises avoiding this as well:
The MATLAB blogs too:
If you want to learn to use use MATLAB efficiently then simply use indexing, and avoid bad practices like eval.

サインインしてコメントする。

 採用された回答

ES
ES 2017 年 3 月 21 日

0 投票

temp = 'abc'
v = matlab.lang.makeValidName(temp)
eval([v,'=5'])

1 件のコメント

Przemyslaw Bibik
Przemyslaw Bibik 2017 年 3 月 21 日
Well, thanks a lot, but I'm a bit ashamed... I've tried that before but didn't work - seems I've made some mistakes. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

質問済み:

2017 年 3 月 21 日

編集済み:

2017 年 3 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by