How do I optionally disable code for a missing toolbox?
2 次查看(过去 30 天)
显示 更早的评论
I am writing code to share with other researchers. I want to use the parallel processing toolbox for speed savings. Other researchers don't have the parallel processing toolbox.
Is there a way for everyone to use the same function? That is, skip or disable the parallel processing functions, if that toolbox is not available? Right now, my only option is to have two versions of every function, which encourages programming errors. When I was programming in C or C++, I would have solved this problem with compile flags. I tried an input flag, but this fails. MATLAB won't run a function that includes parfor if the parallel processing toolbox is missing.
4 个评论
Edric Ellis
2019-6-11
parfor loops should work correctly whether or not Parallel Computing Toolbox is installed. (Other things such as spmd and parfeval do not, unfortunately...). Of course, if PCT is not installed, a parfor loop will run in serial mode.
回答(1 个)
the cyclist
2019-6-10
All credit to Walter, but I believe you'll want to do this test:
license('test','Distrib_Computing_Toolbox')
2 个评论
Walter Roberson
2019-6-10
Exactly.
Note though that this approach has the weakness that it tests whether the user is licensed for something, not whether the thing is installed. There are other approaches that can test whether software is installed, but not whether it is licensed. If you look at the chart I posted, you can use
ver('distcomp')
to probe whether Parallel Computing is installed.
Edric Ellis
2019-6-11
Internally, we tend to use something more like:
isPCTInstalled = exist('gcp', 'file') == 2;
to check whether Parallel Computing Toolbox is installed.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!