MATLAB Answers

Is it not possible to extract a function from code in MATLAB?

9 views (last 30 days)
Martin on 5 Jun 2016
Answered: Matthias SImons on 24 Jun 2021
It would improve my productivity if I could reafactor code into functions automatically. So I want to do this in MATLAB:
void printOwing() {
//print details
System.out.println ("name: " + _name);
System.out.println ("amount " + getOutstanding());
void printOwing() {
void printDetails (double outstanding) {
System.out.println ("name: " + _name);
System.out.println ("amount " + outstanding);

Answers (4)

Geoff Hayes
Geoff Hayes on 5 Jun 2016
Martin - I suspect that it is possible for you to write a MATLAB script that will refactor your Java code so long as you define a clear set of rules. From the above (simple) example, it appears that your MATLAB script would look for comments and convert them to to a function. But would this always be true? Would all comments be automatically turned into functions? At what point would the script stop building the function? When the next comment is reached or the end of the current function?
And in this case, how should the script know that getOutstanding() should be the input parameter to your function and that the input to printDetails should be a double (presumably because that is what getOutstanding returns)? So you will somehow need to keep track of the functions defined in your class (or whatever) file so that you can then use that information elsewhere...
You may want to look at other refactoring tools before starting to write your own MATLAB script to do the same.

Walter Roberson
Walter Roberson on 5 Jun 2016
I would not recommend using MATLAB for this task. I would recommend using a tool optimized for text processing, such as Perl. Or a lot of people like Python for text tasks.

Martin on 5 Jun 2016
Ah guys, I'm sorry. My question was not clear.
I meant that in other IDEs and other languages I can just select some code, right click and automatically generate a function from it (including inputs/outputs). I want to do the same with MATLAB code, but I don't know how. The above example should just explain that functionality.
Hope it is clear now.
a j
a j on 3 Nov 2017
Hi Walter,
I do agree, there are some difficult cases for extracting functions in Matlab. And maybe the example above is not the best since it deals with text instead of math. However, having worked a lot with good Java IDEs (e.g. IntelliJ, Eclipse) the "refactor -> extract method" function is the thing that i miss often in matlab. It can save a lot of headache.
Just my five cents to support the cause.

Sign in to comment.

Matthias SImons
Matthias SImons on 24 Jun 2021
I cannot believe that Matlab has no functionality for this in 2021! I had to use Matlab again after having used Python a lot in the last few years, and I was just stumped that this is missing.

Community Treasure Hunt

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

Start Hunting!

Translated by