Change function name everywhere

38 次查看(过去 30 天)
Marisabel Gonzalez
编辑: Matt J 2024-2-14
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 个评论
madhan ravi
madhan ravi 2019-4-3
编辑:madhan ravi 2019-4-3
Rik please move it to the answer section.
Guillaume
Guillaume 2019-4-3
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.

请先登录,再进行评论。

回答(3 个)

Rik
Rik 2019-4-3
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 个评论
Marisabel Gonzalez
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 2019-4-4
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.

请先登录,再进行评论。


Matt J
Matt J 2019-4-3
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 2024-2-14
编辑:Matt J 2024-2-14
There is a Find-and-Replace app on the File Exchange that can recurse through mfiles in a folder structure and replace text:

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by