File Exchange Pick of the Week

Our best user submissions

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

|
  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。