Change function name everywhere
38 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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 个评论
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/212031/Capture.png)
I can't find any setting in the preferences that would turn that off.
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!