MATLAB Community

MATLAB, community & more

Variable and Function Rename

The MATLAB R2011b feature that I’ve been most anticipating is the automatic rename in the Editor. Renaming functions and variables is a common operation for programmers. It’s useful for making code easier to read and understand. For example, I often start hacking together a script by using easy-to-type names like “a“, “b“, and “a2.” I then piece together a script or function from those commands, but I don’t want to save my code like that–I’d never be able to read what I did in order to understand it! What I really want is to replace “a” with something like “derivedTimes” and “b” with “voltage“.

In the olden days, I used find & replace for my renaming needs. This was fine, but required me to select the variable first, type out the whole new name, and then make sure I didn’t replace any partial words or variables in other functions. The new rename function simply takes care of all that for me. It only rennames matching functions or variables (the search is symbol-based, not text-based) in the same scope. The best part is that you can start typing and it will do the rename of all that matching names in-place… no dialogs or mouse movements needed.

To give it a try, just open up any function or script, palace the caret on a variable name (on the left of an expression), and start typing.

This feature has only a few simple rules when it will appear:

  1. It only renames variable if you type on the left side of an expression or in a function’s argument declaration.
  2. Functions are only renamed from the function declaration.
  3. You can’t rename special variable names like varargin and varargout.

If you don’t want to use this feature and are bothered by the pop-ups, you can disable it from the preferences: File -> Preferences -> Editor/Debugger -> Language -> MATLAB -> “Variable and function renaming”.

For more information, check out the documentation or watch the video.

|

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.