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

8 views (last 30 days)
It would improve my productivity if I could reafactor code into functions automatically. So I want to do this in MATLAB:
void printOwing() {
printBanner();
//print details
System.out.println ("name: " + _name);
System.out.println ("amount " + getOutstanding());
}
|||
|||
vvv
void printOwing() {
printBanner();
printDetails(getOutstanding());
}
void printDetails (double outstanding) {
System.out.println ("name: " + _name);
System.out.println ("amount " + outstanding);
}

Answers (6)

Steven Lord
Steven Lord on 27 Sep 2023
The Release Notes indicate the ability to automatically convert selected code into a function was introduced in release R2021b.
  1 Comment
Luciano Branco
Luciano Branco on 27 Sep 2023
Omg, amazing! I will try this ASAP.
Is there way to add a hotkey for this or just clicking on the Refactor button?

Sign in to comment.


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
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.
  2 Comments
Walter Roberson
Walter Roberson on 5 Jun 2016
So do I understand correctly, you would like to be able to highlight a block of MATLAB code, and have a function automatically generated from it? And the inputs to the function should be all the variables whose value is not defined in the code before first use, and the outputs should be all of the variables assigned to in the code (which might include some of the inputs)?
Would it be necessary to analyze to detect cases where humans can see that a variable would be defined before first use, but where it is not "obvious" that is the case? For example,
for K = 1 : 20
if K == 1
flag = false;
end
flag = ~flag;
end
Here, flag does not need to be an input but a code analysis tool would need to prove that the test that applies to it would definitely be satisfied before use; for example,
for K = 1 : 20
if sqrt(K) == K
flag = false;
end
flag = ~flag;
end
Humans know through mathematical reasoning that on the first iteration, sqrt(1) == 1 so the assignment would happen, but would the code generator be required to do that analysis?
How about
for K = 1 : 20
if cos(pi*K/2) == 0
flag = false;
end
flag = ~flag;
end
Mathematical reasoning tells us that for the first iteration that is cos(pi/2) which we "know" to be 0 so we think the assignment should happen on the first iteration. But in MATLAB, cos(pi/2) is 6.12323399573676603586882014729198302312846062338790031898128063403419218957424163818359375e-17 not 0, because pi is the finite-length truncation of the infinite irrational Pi. It follows from this that to an accurate assessment, the program would need to be emulated to figure out what it does... Oh and suppose the passed-in datatype turns out not to be the expected one?
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.

Luciano Branco
Luciano Branco on 27 Sep 2023
Another day passes and I'm still baffled Matlab doesn't have this functionality...
I get there are difficult edge cases to determine which variables should be input and which can be declared inside the new function. But we can just pass every variable used and not deal with these edge cases @Walter Roberson was mentioning.
  • You select a segment of code, press a hotkey combination and it copies that to a new function at the end of your code.
  • A small GUI asks for function name, maybe confirms variables being 'ported'.
  • It caries the variables you selected and return what the original context needed.
In the original example @Martin provided, it would just take '_name' and 'outstanding' as variables and return nothing.
I'm not too familiar with automating Matlab with things like this but if someone points me in the direction I can give it shot.

Community Treasure Hunt

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

Start Hunting!