Change function name everywhere
46 views (last 30 days)
Show older comments
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 Comments
madhan ravi
on 3 Apr 2019
Edited: madhan ravi
on 3 Apr 2019
Rik please move it to the answer section.
Guillaume
on 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.
Answers (3)
Rik
on 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 Comments
Rik
on 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
I can't find any setting in the preferences that would turn that off.
Matt J
on 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.
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!