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.
There are more than 50 Matlab versions released from 1984 to till date. Matlab 1.0 was the first version released in The Year 1984. However recent versions are written in C, C++, and Java.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With