Renaming Variables
Note
The file submission referenced in this post is no longer available on File Exchange.
Jiro’s pick this week is RENVAR by Duane Hanselman.
Imagine that you defined a variable:
oldVar = magic(3);
Now you want to rename this variable because the meaning of it has changed. How do you do it? Do you do it interactively from
the Workspace Browser?
Or perhaps programmatically:
newVar = oldVar;
clear oldVar
Duane’s renvar let’s you do this in a single swoop.
renvar newVar veryNewVar
If you look inside his function, it is basically executing the two lines from above. I like this for a couple of reasons:
- One line is shorter than two.
- It has a nice nugget of information regarding memory allocation.
From the H1 line:
RENVAR Rename Variable Without Memory Reallocation.
Yes, in fact this method (as well as the two-liner) does not reallocate memory. No temporary memory is used, because it is
doing “lazy copying”. So there’s no need to worry even if you are renaming a very large variable. Take a look at Loren’s blog
posts on Memory Management for Functions and Variables to learn more about “lazy copy”.
Comments
Over the years, I have created many few-line functions, just to make my life easier. What are some of your small, but useful
functions? Tell us here.
Published with MATLAB® 7.6
- 범주:
- Picks
댓글
댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.