Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test for recent-enough version of MATLAB?

Tags:

matlab

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.

like image 964
kjo Avatar asked Mar 26 '14 13:03

kjo


People also ask

How many versions of MATLAB are there?

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.


2 Answers

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
like image 134
gnovice Avatar answered Oct 01 '22 00:10

gnovice


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.

like image 32
Sam Roberts Avatar answered Sep 30 '22 23:09

Sam Roberts