MATLAB Community

MATLAB, community & more

Variable and Function Rename 12

Posted by Michael Katz,

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 are closed.

12 CommentsOldest to Newest

Julian replied on : 1 of 12

I welcome this feature; you describe exactly the process I often use starting with a script with 1-character variable names, evolving into a documented function with sensible names. Until now I always used Find and Replace in this context.

While I never imagined a function like you describe I often thought it would be useful to add check-boxes for “Code”, “Comments”, “String Literals” in addition to those for “Match case”, “Whole word” and “Wrap around”. This would allow more control over the context of the Find & Replace operation.

Terrance M. Nearey replied on : 2 of 12

Great feature! Now how about regex search and replace in Matlab editor. (Yeah, I know. If I want that I should use emacs. But the matlab editor is so PRETTY, on Mac OS 10 anyway.)

John replied on : 3 of 12

Now you know what would make Matlab code much much easier to read? To use different brackets for function calls and array indexing…but that’ll never happen ;) I’ll settle for some python-style string manipulation functions though

Nikolay replied on : 4 of 12

Nice feature indeed.
I’m waiting for a spell checker to be added to Matlab Editor for ages! It sounds both easy and needy (many programs have this feature, take FireFox), but despite my appeals on the company web page, and to Mathworks sales-people, this feature is still out :(

KE replied on : 5 of 12

Like the feature and use it all the time now. Wish it would work for structures (S.temp) as well as variables.

Clovis Bonavides replied on : 6 of 12

It also helped me a lot renaming variables in a >6K lines long script I was even afraid to start because of the existing overlaps between var names and comments….

Ethan Ott replied on : 7 of 12

Will it find and rename references to the variable contained in function arguments taken as strings (as in save(), load(), eval(), etc.)?

Michael Katz replied on : 8 of 12

I wish it were easy. Unfortunately if we did introduce a spell checker, we would want to to work well in all the countries where we ship, be sensitive to scientific and mathematical names, and be syntax-aware. It’s a tall order! It’s not out of the question, but if we were do to this, I couldn’t say when it would be released. But the request has not been forgotten.

@KE and @Clovis,
Thanks! We’re happy this has been useful for you.

Unfortunately not. This version of the tool does not parse into strings.

Justin replied on : 9 of 12

Does this still work in 2012b? I just updated and I can’t seem to find “rename” after right clicking on a variable name, nor do I get the “shift+enter” pop up option when renaming. Major disappointment if this fell through the cracks with the new redesign.

Jiro Doke replied on : 10 of 12


Yes, it’s still in 12b. It only works when you change the definition of the variable or function, i.e. when it’s on the left hand side of the equal (or a function declaration).

Fergal Mullally replied on : 11 of 12

The tool I’d love to see is a function-and-filename rename. I often start writing a function, call it getFoo() and save it in getFoo.m. Then 10 minutes later I realise a better name would be getFooForBar(). I have to change the name, close the editor, rename the file on the command line, and open the newly named file in the editor again.

It’d be so much nicer if I could click File->Rename… in the menu bar

Jiro Doke replied on : 12 of 12


In R2011a, you can do something similar to what you’re looking for.

If you change the file name in the Current Folder Browser, the file open in the Editor will automatically update so that you don’t have to close and reopen the file. Then, you can just change the function declaration line with the new name (use the Auto-Fix feature).

Note: this won’t work if you change the file name from the Command Window