Feature Request: Refactoring tool

19 views (last 30 days)
Nils G
Nils G on 21 Nov 2016
Commented: attackers account on 11 Aug 2022
I would like to have a real refactoring tool for MATLAB like the one e.g. in PyCharm. At the moment I'm using Grep if I'm renaming a Class/Method/Function.

Answers (7)

Grivault
Grivault on 17 Oct 2018
I agree, the lack of this refactoring capability is making Matlab IDE quite outdated face to other IDEs... As it seems to me impossible to develop Matlab with another IDE it makes Matlab also non-competitive and less attractive face to other languages (python, java, C# etc...) Missing syntactic coloration is already from another age, but since it's intrinsic to Matlab we can accept it. Refactoring though is the minima nowadays.

Adam
Adam on 2 Apr 2019
This is still such a huge frustration all the way into R2019a. Even things that should be so simple can take on epic proportions when it comes to simple changes.
In one of my latest classes I didn't think ahead far enough and named some public functions things like
getData(...)
getScaledData(...)
getTrainingData(...)
Now, of course, a few weeks later I am incorporating a 2nd type of data that I didn't expect originally. So now I have to rename these functions to something more specific than just 'Data'. But they are public functions so I now have to search the whole repository with a Find All. This is not so bad for getTrainingData(...), I don't have many occurences of this, but I have numerous classes with a getData(...) function and loads of places that call it. The class/object of the call gives them their scope so the functions don't need more specific names often.
Except now when I come to have to do a rename and I have to sift through all those cases to make sure I only change those related to the class I am editing.
My design may not be great in terms of having these functions where they are, but isn't that the whole point of refactoring? You don't need to do perfect upfront design, you evolve it as appropriate!
This is one of the most basic tasks of refactoring too. I have no idea how easy or difficult is is for Matlab to link these in an interpreted language, but other languages seem to do it and even Matlab itself I have noticed has improved a lot in terms of its resolution if I 'Go to definition' on a function and it manages to take me to the relevant class.
  3 Comments
Adam
Adam on 8 Apr 2019
Yeah, Ctrl + Shift + F is the method I've always used (it has been around as long as I remember). It helps, but still getting a list of 100+ matches when only 4 of them are related to the specific occurence I want to change makes refactoring a slow risky business!

Sign in to comment.


Frank
Frank on 8 Oct 2018
Dear Don,
Nils asked about refactoring and not how to rename a local variable. The lack of support for refactoring within Matlab is quite annoying.
-Frank

Keith Nguyen
Keith Nguyen on 9 Nov 2018
This is true. If I want to rename a function (to give it an easy-to-understand name instead of test(ptcle1,ptcle2). I'll break my program. Is there a way to rename the function across all files in the same project/folder/workspace?
Other features such as: - Changing function signature also change it across all files. - Moving lines up and down using shortcut. - Syntax highlighting.

Tom Pickering
Tom Pickering on 23 Sep 2020
Hi Nils,
if you rename the function name in the function declaration, a tooltip opens. The tooltip indicates that MATLAB will rename all instances of the function in the file when you press Shift + Enter.
Please also check out the Documentation for more info on Find and Replace in Files.
In addition to this functioncality in both the classic Editor and the Live Editor, the Live Editor also has some additional refactoring features. If you have an existing large live script or function, you can break it into smaller pieces by automatically converting selected areas of code into functions or local functions. Select one or more lines of code and on the Live Editor tab, in the Code section, click Refactor. Then, select from the available options. MATLAB creates a function with the selected code and replaces the original code with a call to the newly created function.
In Projects, when renaming a function, a dependency analysis is performed, notifying you which files and lines are impacted before you make a change.
  3 Comments
Jared MacDonald
Jared MacDonald on 10 Nov 2020
Hi Vasco, this is what we are focused on (migrating features from Live Editor to be available for your .M files) and is planned for a near upcoming release.
Thanks,
Jared
Walter Roberson
Walter Roberson on 23 Sep 2021
As of R2021b, released today, the old editor is retired and the same editor used for Livescript is used for .m files.
... Unfortunately this does mean that the Fix options that used to be available are mostly gone :(

Sign in to comment.


Don Zheng
Don Zheng on 7 Dec 2016
Hi Nils,
Please find the tutorial below for code refactoring in MATLAB Editor:
-Don
  2 Comments
Luke M
Luke M on 7 Aug 2018
Edited: Luke M on 7 Aug 2018
I know this is a fairly old question, but I haven't found anything more recent. I think what Nils is looking for is a tool to refactor a project when renaming, as they said, a Class, Method or Function. The tutorial presented is great when working in a single file, but what about when working on a project with many files?
Utku Kaya
Utku Kaya on 26 Oct 2021
Edited: Utku Kaya on 26 Oct 2021
Agreed. Refactoring still does not work for Classes.

Sign in to comment.


Michael W
Michael W on 23 Sep 2021
It seems some added functionality is now available in the editor since release R2021b:
(from the R2021b release notes)
"Break large scripts or functions into smaller pieces by converting selected code into functions in files or local functions. With one or more lines of code selected, on the Editor tab, in the Code section, click the Refactor button, and then select from the available options. MATLAB creates a function with the selected code and replaces the original code with a call to the newly created function."
  1 Comment
Nikolaus Koopmann
Nikolaus Koopmann on 5 Jan 2022
doesn't work in classes. hence, utterly useless to me :(

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!