Is there exist() functionality for packages/namespaces?

13 次查看(过去 30 天)
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.

采纳的回答

Ashley Trowell
Ashley Trowell 2023-10-26
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.

更多回答(1 个)

Walter Roberson
Walter Roberson 2023-1-6
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 个评论
Chris
Chris 2023-1-7
编辑:Chris 2023-1-7
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.
Walter Roberson
Walter Roberson 2023-1-7
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)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by