Reputation: 35301
A function I want to implement needs to know whether the current version of MATLAB is at least as recent as R2014a
.
Is there a robust, supported way to perform this check?
(With "robust, supported" I mean to say that I'm not interested in fragile hacks such as parsing the string returned by version, etc.)
BTW, in this case, the reason I want this check is to know that I can use the function matlab.lang.makeUniqueStrings
. If there were a robust, supported way to check for the availability of this function, I'd use it instead of testing that the current MATLAB is recent enough. Unfortunately, there doesn't seem to be such a check: exist
returns false to every variant I can come up for the name of this function. Again, I can think of fragile hacks to mimic a proper test (e.g. which('matlab.lang.makeUniqueStrings')
), but they're hardly better than the version-testing hacks I alluded to above.
The best solution I have found is to run the command using matlab.lang.makeUniqueStrings
within a try-catch
block. This is still a fragile hack, because MATLAB
does not offer a robust, built-in way to catch specific exceptions!
IOW, it's all about choosing the least awful hack. Testing that the current version is recent enough (even if this test is a fragile hack) at least has the virtue of being general enough to stick in some function, and at least contain the proliferation of fragile, hacky code.
Upvotes: 9
Views: 4104
Reputation: 38032
An example of what Sam meant:
try
%// call to matlab.lang.makeUniqueStrings
catch ME
%// (use regexp here to include support for Octave)
if strcmpi(ME.identifier, 'MATLAB:undefinedVarOrClass')
error('yourFcn:someID',...
'matlab.lang.makeUniqueStrings is not supported on your version of MATLAB.');
else
throw(ME);
end
end
Robust until The MathWorks changes the ID string.
As a final remark: checking for features is not sufficient: what if The MathWorks decides to change the function signature? Or the output argument list? Or ..?
There is no really robust method in a language that is itself not robust. Be as robust as the language allows you, but no more.
Upvotes: 2
Reputation: 13081
Testing for version number is barely a good idea. You should always check for features, and never for versions, if you really want robustness (and at the same time portability).
What will happen if one of the features you need is removed from a future version of Matlab? Or the way it works changes? (this is far more common than one would expect). Or if someone wants to use your code in a Matlab compatible system that does have the features your code requires?
There are some autoconf macros related to Matlab around (although I have never used one). Or you can write your own simply checks in Matlab language.
Upvotes: 0
Reputation: 24127
If you only need to care about fairly recent versions, use the verLessThan
command. However, verLessThan
was introduced in about 2006a or so; if you need to support versions older than that, you will need to use the output of the version
command.
Alternatively, you can robustly test for the existence of matlab.lang.makeUniqueStrings
. Firstly, use m = meta.package.fromName('matlab.lang')
to retrieve a meta.package
object referring to the package. If m
is empty, the package does not exist. Assuming m
is not empty, check the FunctionList
property of m
to see whether makeUniqueStrings
is present. (There's also a ClassList
property as well).
Finally, MATLAB does offer a way to catch specific exceptions. Instead of a simple catch
, use catch myError
. The variable myError
will be an object of type MException
, available within the catch
block. You can test the identifier
and message
properties of the exception, and handle different exceptions appropriately, including rethrowing unhandled ones.
Upvotes: 8
Reputation: 125854
I would use the verLessThan
function:
verLessThan('matlab', '8.3')
This will return true (1) if the current version you are using is older than 8.3 (R2014a) and false (0) otherwise. No string parsing required.
You could then use it like so:
if ~verLessThan('matlab', '8.3')
% Run code using matlab.lang.makeUniqueStrings
end
Upvotes: 17
Reputation: 221504
You may use MATLAB command version
for your test -
['Release R' version('-release')]
Sample run -
>> ['Release R' version('-release')]
ans =
Release R2012a
Check if your MATLAB version is the recent one (R2014a) -
strcmp (version('-release'),'R2014a')
The above command would return 1
if it's a recent version, otherwise returns 0
.
Upvotes: 3
Reputation: 39698
The best way is to use the version
command, and parse the string appropriately.
[v d] = version
Take a look at the output from R2014a, and set your values appropriately.
Upvotes: 2