Using vecnorm function as defined in a toolbox

4 views (last 30 days)
I am using a toolbox called MNPBEM17, which includes a function called vecnorm. Its code is as follows:
function n = vecnorm( v, key )
% VECNORM - Norm of vector array.
%
% Usage :
% n = vecnorm( v )
% n = vecnorm( v, 'max' )
% Input
% v : vector array of size (:,3,siz)
% Output
% n : norm array of size (:,siz)
% or maximum of N if 'max' set
n = squeeze( sqrt( dot( abs( v ), abs( v ), 2 ) ) );
% maximum of N for 'max' keyword
if exist( 'key', 'var' ) && strcmp( key, 'max' ), n = max( n( : ) ); end
This function has the same name as a builtin function (https://nl.mathworks.com/help/matlab/ref/vecnorm.html) which works a little differently, but in any script I write, I want to use the version from the toolbox.
When launching Matlab 2019a I always get "Warning: Function vecnorm has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict." I don't want to change the name of the toolbox function though because I don't want to change any references to it throughout the toolbox. In Matlab 2019a, this is apparently not a problem and the toolbox function is always called:
v = rand(5,3);
a = squeeze( sqrt( dot( abs( v ), abs( v ), 2 ) ) )
b = vecnorm(v)
a =
0.8355
1.3565
1.1097
1.4095
1.4040
b =
0.8355
1.3565
1.1097
1.4095
1.4040
However, I recently installed Matlab 2021b and now it doesn't work that way. I put the toolbox at the top of the path. The commands "help vecnorm" and "open vecnorm" refer to the toolbox version, but the output of the same code as above is:
a =
0.8355
1.3565
1.1097
1.4095
1.4040
b =
1.6536 1.4947 1.6611
I deleted every file called vecnorm.* from the 2021b installation except the one from the toolbox. Still, the vecnorm function keeps giving me this undesired output.
Any ideas on how to persuade Matlab to use the vecnorm function from the toolbox?
  6 Comments
Matt J
Matt J on 8 Oct 2021
Edited: Matt J on 8 Oct 2021
Intriguing (and worrisome). I see it in 2020b as well...
Well, it's probably for the best. You don't know what effects overriding a built-in Matlab function can have. Many other stock Matlab functions could rely on the built-in version.
Sjoerd Nooteboom
Sjoerd Nooteboom on 25 Oct 2021
Sorry for the late reply. I will rename the function and all the calls to it, that seems like the most robust solution to me. Thanks for the input.
(I can't accept answers which are in a comment, can I?)

Sign in to comment.

Answers (2)

Matt J
Matt J on 8 Oct 2021
One solution I've found is to put vecnorm in a package directoy, e.g. +package/vecnorm.
If you then import the version of vecnorm from the package,
import package.vecnorm
then that version will be used (until you clear the import of course).

Matt J
Matt J on 8 Oct 2021
Edited: Matt J on 8 Oct 2021
Another solution might be to rename
MNPBEM17\Misc\vecnorm.m
to
MNPBEM17\private\vecnorm.m
This will only work, however, if the parent folder MNPBEM17\ contains all the files that use vecnorm.

Categories

Find more on Linear Algebra 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!