Skip to content

Commit 8086f9b

Browse files
committed
fallback
1 parent 4da655e commit 8086f9b

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

+stdlib/checkout_license.m

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
% that is, when it's certain the function next called requires this license
66
% to be checked out.
77
%
8-
% This function provides a more accurate indicator that a function can
8+
% A simpler way is to just use the desired function enclosed with try-catch
9+
%
10+
% This function better tells that a function can
911
% be used rather than just checking if the relevant toolbox is installed.
1012

1113
function [ok, featureName] = checkout_license(packageName)
@@ -19,25 +21,45 @@
1921
% feature('webui') is false for Matlab < R2025a if not using the "New Desktop"
2022
try
2123
addons = matlab.addons.installedAddons;
22-
installedPackages = addons.Name;
23-
name = addons.Name(strcmpi(installedPackages, packageName));
24+
name = addons.Name(strcmpi(addons.Name, packageName));
2425
catch
25-
warning('stdlib:checkout_license:EnvironmentError', 'Unable to retrieve installed addons.')
26-
return
26+
versions = ver();
27+
installedPackages = string({versions.Name});
28+
name = installedPackages(strcmp(installedPackages, packageName));
2729
end
2830

29-
if isempty(name), return, end
31+
if isempty(name)
32+
disp(name + " not installed in Matlab " + matlabRelease().Release+ " at " + matlabroot)
33+
return
34+
end
3035

3136
if ~usejava('jvm')
32-
warning('stdlib:checkout_license:EnvironmentError', 'Java must be enabled to lookup license names.')
37+
disp('Java must be enabled to lookup license names.')
3338
return
3439
end
3540

36-
% https://www.mathworks.com/help/matlab/matlab_env/index-of-code-analyzer-checks.html
37-
featureName = string(com.mathworks.product.util.ProductIdentifier.get(name).getFlexName()); %#ok<JAPIMATHWORKS>
41+
featureName = product2feature(name);
3842

3943
if license('test', featureName)
4044
ok = license('checkout', featureName);
4145
end
4246

4347
end
48+
49+
50+
function f = product2feature(name)
51+
52+
% https://www.mathworks.com/matlabcentral/answers/195425-how-do-i-get-a-license-feature-name-for-a-toolbox-in-ver#answer_173402
53+
% https://www.mathworks.com/matlabcentral/answers/152553-how-can-i-obtain-a-feature-name-or-product-name-within-matlab#answer_150023
54+
% https://www.mathworks.com/help/matlab/matlab_env/index-of-code-analyzer-checks.html
55+
56+
f = com.mathworks.product.util.ProductIdentifier.get(name).getFlexName().string; %#ok<JAPIMATHWORKS>
57+
58+
end
59+
60+
61+
function p = feature2product(name) %#ok<DEFNU>
62+
63+
p = com.mathworks.product.util.ProductIdentifier.get(name).getName().string; %#ok<JAPIMATHWORKS>
64+
65+
end

0 commit comments

Comments
 (0)