Is there exist() functionality for packages/namespaces?

Create a package and put it somewhere in Matlab's path. If you know the parent directory, you could use exist('/path/+package'), but you don't need to know the path to use the package, so that's silly.
The current best solution I have is to try/catch calling a function in the package.

 Réponse acceptée

Ashley Trowell
Ashley Trowell le 26 Oct 2023

2 votes

There is, but it is hidden. Mathworks doesn't encourage packages and namespaces.
Create this lambda function:
packageExists = @(pkgName) ~isempty(meta.package.fromName(pkgName));
Then run
pkgExist = packageExists('myPkg');
A nice utility is here that just adds the functionality the the exist function, which is what Mathworks should do.

Plus de réponses (1)

You can exist() a function inside the package
exist('matlab.internal.lang.capability.Capability')
ans = 8
which matlab.internal.lang.capability.Capability
/MATLAB/toolbox/matlab/capabilities/+matlab/+internal/+lang/+capability/Capability.m % matlab.internal.lang.capability.Capability constructor
You can also which the + form of a function (but not the package itself)
which +matlab/+internal/+lang/+capability/Capability
/MATLAB/toolbox/matlab/capabilities/+matlab/+internal/+lang/+capability/Capability.m % matlab.internal.lang.capability.Capability constructor
which +matlab/+internal/+lang/+capability
'+matlab/+internal/+lang/+capability' not found.

5 commentaires

Interesting. My package is organized into @Folders containing classes and their methods, and some functions in the base +folder.
exist will look for a file, folder, or class, which I suppose is supported by the results below--results that were confounding my efforts to use exist.
which is supposed to "Locate functions and files" but works for everything but packages and class methods (as long as it's formatted properly)
I can almost see the logic.
Things that work:
which package.function
which package.Class
which +package/function
which +package/@Class/Class
which +package/@Class/function
exist package.Class
exist +package/function
exist +package/@Class/Class
exist +package/@Class/function
Things that don't work:
which package
which +package
which package.Class.function
exist package
exist +package
exist package.function
exist package.Class.function
matlab.internal.lang.capability.Capability is an example of package.function but which and exist work for it ? (notice it is + all the way down to +capability so matlab.internal.lang.capability is the package and Capability is the function)
It looks like a classdef to me, no? Its constructor would be a method by the same name, but it's not a standalone function.
help matlab.internal.lang.capability.Capability
Capability List of optional execution capabilities which may be enabled or disabled at runtime. Documentation for matlab.internal.lang.capability.Capability doc matlab.internal.lang.capability.Capability
Chris
Chris le 7 Jan 2023
Modifié(e) : Chris le 7 Jan 2023
The difference compared to my package would be that the classdef file is not in an @ folder, but in the package root. So it's another example of
exist package.Class
that works.
I wouldn't expect a class method to be available to exist or which (though it turns out they can be found if treated as files), so that just leaves the package itself (I can live without that, I guess), and functions in the +folder--which are the one thing treated differently between which and exist.
Class methods can be found using which, but sometimes they will not be located unless the class has been loaded at least once in this session (since the last clear classes)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Software Development Tools dans Centre d'aide et File Exchange

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by