How to Rename all variables

조회 수: 10 (최근 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 CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by