How to Rename all variables

3 次查看(过去 30 天)
Nick van der maat
编辑: 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 CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by