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:
- It only renames variable if you type on the left side of an expression or in a function’s argument declaration.
- Functions are only renamed from the function declaration.
- 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”.
12 CommentsOldest to Newest
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.
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.)
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
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 :(
Like the feature and use it all the time now. Wish it would work for structures (S.temp) as well as variables.
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….
Will it find and rename references to the variable contained in function arguments taken as strings (as in save(), load(), eval(), etc.)?
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.
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.
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).
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
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