How to update variable names after a "clear"

Dear Community,
I wonder if there is another way to clear variables. If I do:
clear var1 var2
% or
clearvars('var1','var2')
The variables are strings or purple and are not updated if the name of the variable is changed.
How can I realize that variables after a clear or clearvars are updated if the variable name is changed. (e.g. the original variable name was not descriptive enough).
Edit: I mean not during running the code, the variable name is not dynamic! I mean during writing, the editor offers you the possibility to rename all instances of a variable (Shift-Enter)
>> Press Shift+Enter to rename 4 instances of " var1" to " anothernameOfVar1"
within a script. Variables after a clear or clearvars are not affected by this.
Best, Robin

댓글 수: 9

KSSV
KSSV 2017년 10월 17일
After you clear there is no more variable present. You have cleared the variable, how you expect it to update?
Stephen23
Stephen23 2017년 10월 17일
편집: Stephen23 2017년 10월 17일
@Robin H: after you clear those variables they do not exist any more, so they cannot be "updated" (whatever that means). Not only that, clearing variables is slow, and is very rarely required in well written MATLAB code. Most often clear is massively overused by beginners as a kind of cargo-cult programming.
"...if the variable name is changed. (e.g. the original variable name was not descriptive enough)"
This sounds very much like that you are magically changing variable names, and including some kind of meta-data in those variable names. That is a very inefficient and buggy way to write code:
N/A
N/A 2017년 10월 17일
with updated I mean that if I change the name of a variable (rename it) that the name of the variable is renamed after the clear aswell (as all other instances of this variable before the clear)
KSSV
KSSV 2017년 10월 17일
If you want to clear the variable itself..why do you want to rename it to other variable? What exactly you are trying?
N/A
N/A 2017년 10월 17일
I mean not during code running, but during writing. Matlab offers you to rename all instances of a variable (Shift-Enter) once you change the name. Variable names after a clear are not affected by this.
Rik
Rik 2017년 10월 17일
I don't have a solution for you, and I agree that you shouldn't need clear in the first place, but your question still stands with functions like exist, where you might also use a variable name in a string. The only method I know is just doing it by hand.
After a clear x, the variable "x" is not longer existing. Then it would be meaningless if the editor changes later occurrences. This is a logical behavior:
x = 19
clear x
x = 'hello'
Now renaming the initial x should stop after the 2nd line.
Rik
Rik 2017년 10월 17일
Except that this isn't how it works (at least in R2017b). ALL occurrences of x are replaced, except for the occurrence as a string (either in exist or clear)
@Jan, you'd have a point if that is what was happening. Unfortunately, the automatic renaming is indeed broken. Just tested the following (R2017b online):
x = 5;
a = x;
clear x %unnecessary clear
x = 10;
b = x;
Then renamed the first x to y and pressed shift+enter for automatic renaming and ended up with
y = 5;
a = y;
clear x %unnecessary clear
y = 10;
b = y;
So it renames all instances of x regardless of whether or not they're actually the same variable but doesn't rename it in the clear call.
The reason is most likely because in that call x is actually the char array 'x' (since this is the command form of clear) and automatic renaming does not look in strings (or comments for that matter).

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2017년 10월 17일
편집: Rik 2019년 7월 17일

0 개 추천

Unfortunately, this is a limitation of matlab's editor.
Note that
clear x
is exactly the same as
clear('x')
The former is the command form of the latter. In both cases, the input to clear is the char array 'x'.
The automatic renaming does not look in char arrays, strings or comments. So as Rik commented, the only way to deal with that is by doing the renaming yourself.
Matlab refactoring tools have thankfully improved over the years but are still way behind what is available in other editors (It really pales when compared to Visual Studio).

댓글 수: 1

N/A
N/A 2017년 10월 17일
편집: N/A 2017년 10월 17일
I mark this answer, even though it is not the solution due to the limitations of the editor. Might be irrelevant in a future release.
Thanks all

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2017년 10월 17일
편집: Jan 2019년 7월 17일

0 개 추천

Clearing variables is rarely useful in Matlab, but a waste of time only. So on one hand it is not exactly clear, what you are asking for, but take into account to simply remove this line.

댓글 수: 3

N/A
N/A 2017년 10월 17일
I made a edit on my question to make more clear hopefully.
I mean during writing Matlab editor offers you to rename all instances of a variable (Shift-Enter) once you change the name. Variable names after a clear (or "exist" as Rik pointed out) are not affected by this.
Stephen23
Stephen23 2017년 10월 17일
편집: Stephen23 2017년 10월 17일
@Robin H: Note that the variables after clear are different variables, because after clear that new variable (likely) uses a different memory location and (likely) contains different data. The fact that you have coincidentally used the same name is irrelevant.
Jan
Jan 2019년 7월 19일
@Robin H: I understood. And if you want to replace all occurences, you can use NotePad++. I've written a Matlab function to replace strings in M-files. It is a little bit tricky to distinguish quoted and double quoted strings from names of variables, but not magic. I avoid te non-functional form of commands like clear x, but I do not have any needs for clear commands at all.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

질문:

N/A
2017년 10월 17일

댓글:

Jan
2019년 7월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by