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
1113function [ok , featureName ] = checkout_license(packageName )
1921% feature('webui') is false for Matlab < R2025a if not using the "New Desktop"
2022try
2123 addons = matlab .addons .installedAddons ;
22- installedPackages = addons .Name ;
23- name = addons .Name(strcmpi(installedPackages , packageName ));
24+ name = addons .Name(strcmpi(addons .Name , packageName ));
2425catch
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 ));
2729end
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
3136if ~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
3439end
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
3943if license(' test' , featureName )
4044 ok = license(' checkout' , featureName );
4145end
4246
4347end
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