14 Jun 2018

Get Matlab Version

收藏到CSDN网摘

Matlab provides loads of useful functions for scientific computing. However, the functions might be added or deprecated from version to version. To write more robust codes, it's always a good practice to deal with different cases. An if...else... clause would be enough for this purpose.


Matlab does have a 'version()' function that returns a string of the version information like '9.1.0.441655 (R2016b)' - the last character might be 'a' or 'b' depending on your release. All we need to know is the content inside the brackets '2016b'. But the character/string is not very convenient for comparison though it's doable.

I prefer to convert the string to a numeric value, which is easier to be used in the 'if...else...' clause. The idea is to convert 'a' to 0 and 'b' to 1. Then, they could be appended as the last digits of the main version string value. For instance, '2016a' would be converted to 20160 and '2014b' would be converted to 20141. By using this simple conversion, one could use some structures as:
v = GET_VERSION();
if v<20141
    % call functions for versions before 2014b
elseif v<20160
    % call functions for versions from 2014b to 2016a
...
The GET_VERSION() function could be written as:
function v = GET_VERSION()
v = regexp(version,'\(R(\d+[ab]*)\)','tokens');
vStr = v{1}{1};
if length(vStr)==4
   v = str2double(vStr)*10;
else
   subVer = vStr(end)-'a';
   v = str2double(vStr(1:end-1))*10+subVer;
end
end

No comments :

Post a Comment