Change function name everywhere

51 visualizzazioni (ultimi 30 giorni)
Marisabel Gonzalez
Marisabel Gonzalez il 3 Apr 2019
Modificato: Matt J il 14 Feb 2024
Hello! I was wondering if there is a way to change the name of a function and simultanously change it in the scripts where it is called. Or alternatevely, can I change the name of a function called multiple times in the same script at once as we do for variable names?
Thanks
  3 Commenti
madhan ravi
madhan ravi il 3 Apr 2019
Modificato: madhan ravi il 3 Apr 2019
Rik please move it to the answer section.
Guillaume
Guillaume il 3 Apr 2019
Hopefully, now that matlab has projects, the change can be project wide. I've not really had a chance to test projects yet.
Certainly, pre-R2019, there's no all-files wide way to change a function name. In a way that makes sense, there's no way for matlab to know that funA called by fileX is the same funA that is called by fileY.

Accedi per commentare.

Risposte (3)

Rik
Rik il 3 Apr 2019
If you change the name in the function declaration, the auto-change should work, just as with variable names inside the same scope. The limitation for this is that this only works inside the same file.
You can use the dependecy analyzer to find where your function is called to change every file.
I would have expected the dependency report generator to be easy to find in the doc, but that is unfortunately not the case. You can find it in the editor. In the same drop-down where you can choose to dock or undock the edit you will find a menu option to "show dependency report". There you can select the checkbox "show parent functions" and click the button "run report on current folder".
  2 Commenti
Marisabel Gonzalez
Marisabel Gonzalez il 4 Apr 2019
For some reason I never see the auto-change for functions as I do for variables. Any idea why? It does not even gets highlighted as it does for variables...
Rik
Rik il 4 Apr 2019
I just checked on my copy of R2018a and for me that highlighting works. If I put the cursor in the someFunction definition (inside the name), the call in Untitled becomes highlighted blue as well.
function Untitled
someCode
a=someFunction;
disp(a)
end
function a=someFunction
a=1;
end
Capture.PNG
I can't find any setting in the preferences that would turn that off.

Accedi per commentare.


Matt J
Matt J il 3 Apr 2019
Hello! I was wondering if there is a way to change the name of a function and simultanously change it in the scripts where it is called.
It is probably better just to create an alias for the original file
function varargout=newfunction(varargin)
[varargout{1:nargout}]=originalfunction(varargin{:});
end
Now, newfunction() and originalfunction() are equivalent and you don't have to go tracking down and changing every location in the code where it was called.

Matt J
Matt J il 14 Feb 2024
Modificato: Matt J il 14 Feb 2024
There is a Find-and-Replace app on the File Exchange that can recurse through mfiles in a folder structure and replace text:

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by