Automatic Refactoring of Classes

8 views (last 30 days)
Jose Gonzalez
Jose Gonzalez on 22 May 2018
Answered: per isakson on 18 Dec 2019
I am working in a project that is using multiple classes and I have the need to refactor (rename) some of the classes and functions within those classes. Using an example, I will illustrate the problem that I am facing.
• Let us say that I have two classes in the project. One is called “Class1” and another one is called “Class2”. “Class1” calls ““Class2” and some of its functions.
• I have the need to rename “Class2” to “Class3”. If I do so, that creates a problem in “Class1”. “Class1” will still be referencing “Class2”. Therefore my code does not compile.
• In order to solve this issue I need to go to “Class1” and rename every instance that was referring to “Class2” and substitute it for “Class3”.
• Other languages (or their IDEs) automatically make the change for you. I wanted to know if there was a way to do the same in Matlab.
• If the class is used several times in different classes of the project, refactoring can become a burdensome / extremely difficult task.
Matlab has similar functionality for renaming variables within the same file. Renaming and then pressing “Shift + Enter” does the trick within the same file. Therefore, I am looking for an equivalent solution when you are working with different classes/files in the same project.

Accepted Answer

Guillaume
Guillaume on 22 May 2018
Unfortunately, the refactoring tools are very limited in matlab. I'm afraid you're left with search and replace. You'll have the same problem if you rename packages.
The other issue you may run into is that you won't be able to reload old class instances stored in mat files, even if you try to overload loadobj.

More Answers (1)

per isakson
per isakson on 18 Dec 2019
There are some submission in the File Exchange that should be useful for renaming names over many files. Search for replace in files.
I have successfully used Find and Replace in Files by Robyn Jackey [Staff] to change names in many files.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!