Using vecnorm function as defined in a toolbox

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 Kommentare

The easiest option would be to rename the user-supplied function, perhaps to ‘vecnormk’ or something appropriately similar. This requires changing every previous reference to it in existing code, however there may be no other options.
Since it worked as desired in 2019a without having to rename the function, and since help and open do refer to the toolbox version, I hoped there might be an even easier way... But it seems that renaming may be the best solution after all.
Matt J
Matt J am 8 Okt. 2021
Bearbeitet: Matt J am 8 Okt. 2021
I put the toolbox at the top of the path.
That doesn't seem possible. Perhaps you didn't save the path? What does the which() command say?
>> which -all vecnorm
I did save the path, the toolbox folder and its subfolders are at the top. The output of that command is:
C:\Program Files\MATLAB\R2021b\toolbox\MNPBEM17\Misc\vecnorm.m
built-in (C:\Program Files\MATLAB\R2021b\toolbox\matlab\matfun\@double\vecnorm) % double method
built-in (C:\Program Files\MATLAB\R2021b\toolbox\matlab\matfun\@single\vecnorm) % single method
C:\Program Files\MATLAB\R2021b\toolbox\matlab\bigdata\@tall\vecnorm.m % tall method
built-in (C:\Program Files\MATLAB\R2021b\toolbox\matlab\matfun\vecnorm) % Shadowed
So the version I want is above all the other versions. Also, I just tested what happens if I put MNPBEM17\Misc at the bottom of the path: it moves to the bottom of the which() output, and help and open then refer to the builtin version. So the path definitely has an effect, Matlab just keeps using the builtin version when I call the function itself...
Matt J
Matt J am 8 Okt. 2021
Bearbeitet: Matt J am 8 Okt. 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.
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?)

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Matt J
Matt J am 8 Okt. 2021

0 Stimmen

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 am 8 Okt. 2021
Bearbeitet: Matt J am 8 Okt. 2021

0 Stimmen

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.

Kategorien

Mehr zu Linear Algebra finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 7 Okt. 2021

Kommentiert:

am 25 Okt. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by