How to Rename all variables

2 ビュー (過去 30 日間)
Nick van der maat
Nick van der maat 2015 年 5 月 6 日
編集済み: Stephen23 2019 年 6 月 19 日
hello,
If a certain if statement is true, i want to rename alle the variables that contains "MAIN_" to "New Name". (All vectors)
Is there an easy way to do this? I tried the rename function but that does not work with double's.

回答 (2 件)

Guillaume
Guillaume 2015 年 5 月 6 日
A variable can't be renamed. All you can do is create a copy of the old one with a new name and then clear the old one:
NewName = Main;
clear Main;
Due to the way matlab works, there won't be any copy actually.
However, there shouldn't be any reason for you to want to rename variables in the first place. If you do, it's most likely because you've been using dynamic variable names, which is a bad idea in the first place

Stephen23
Stephen23 2015 年 5 月 6 日
編集済み: Stephen23 2019 年 6 月 19 日
  4 件のコメント
Stephen23
Stephen23 2015 年 5 月 6 日
編集済み: Stephen23 2015 年 5 月 6 日
@Nick van der maat: yes, we know what you are trying to do and that is exactly what we are giving advice about. It is considered to be a bad programming practice generally: variables should not just pop into existence like that. Just because you can do something does not make it a good idea!
If you take the time to read those links then people with more knowledge than me will explain why dynamically creating variables is a bad idea.
John D'Errico
John D'Errico 2015 年 5 月 6 日
編集済み: John D'Errico 2015 年 5 月 6 日
@Nick - you are getting good advice from both responders. Please learn from it.
Your code will improve greatly, becoming more robust to bugs. It will be faster to write your code, no longer to need many different variables with all those names. And you can avoid all those otherwise necessary evals, a bad thing in general to use.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by