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

1 visualización (últimos 30 días)
Martin
Martin el 5 de Jun. de 2016
Comentada: Luciano Branco el 27 de Sept. de 2023
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);
}

Respuestas (6)

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

Iniciar sesión para comentar.


Geoff Hayes
Geoff Hayes el 5 de Jun. de 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 el 5 de Jun. de 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 el 5 de Jun. de 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 comentarios
Walter Roberson
Walter Roberson el 5 de Jun. de 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 el 3 de Nov. de 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.

Iniciar sesión para comentar.


Matthias SImons
Matthias SImons el 24 de Jun. de 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 el 27 de Sept. de 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.

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by