vernum - returns the current version of Matlab as a number
30 June 2006 - Created
by David S. Ciochetto and Audrey Barnett, Dalhousie Oceanography
30 June 2006 (Last modified) by David S. Ciochetto
This function gets the current version of matlab and converts it to a real number through the first decimal point. It is intended to be used to delimit cases where code is specific to particular Matlab versions. It is particularly useful for < or > a specific version such as when someone writes to you and says hey, this does not work for Matlab 3... you can use vernum in your code to add a patch with case or if statements to write code that works for the older versions.
It is intended to make detection of the version automatic in your code so it keeps the users of your scripts happy since they do not need to go in and fix the code for their specific version.
if vernum < 7.1 % The below works for Matlab versions earlier than 7.1
% Add code
else % works for newer code
% Add fancy code here
Dave Ciochetto (2021). vernum.m (https://www.mathworks.com/matlabcentral/fileexchange/11602-vernum-m), MATLAB Central File Exchange. Retrieved .
For 2010a this replies 7.10, which equals 7.1!
Faster and simpler (and less than 2 lines):
VersionNumber = sscanf(version, '%f', 1)
But this fails for v7.10 also, but this get catch it:
[10000, 100, 1] * sscanf(version, '%d.', 3)
e.g. 6.5.1 => 60501. This is monotonically increasing and unique - but still just another strange method.
Jos's comment about computing vernum will work on any MATLAB that has struct's. However, the whole point of a function like this is that it must work on very old MATLAB releases, including very old MATLAB's that do not have structs.
I think the .Version field returned from ver might be a string of the form 'A.B.C', in which case Jos' suggestion will fail completely.
In any case, "ver" is about 100 times slower than "version" (try it), an important consideration if this code is called inside a heavily-called function. That critique applies to the new "verLessThan" function in MATLAB, too, by the way.
So a well-written code, like this, must not use "ver".
The "version" function is likely to exist on much older MATLAB releases than "ver", since the former does not use structs.
The real question, though is this: will this code work on MATLAB 1, 2, 3, 4, 5, ...? How far back has it been tested?
I rate it a 4 because the code should be cleaned up a bit, the comments ("see also", for example) are lacking, and (mostly) because it isn't clear from the documentation how far back the code has been tested. It would also be nice to be able to distinguish between 7.0.1 and 7.0.4.
I have to strongly disagree with Sergei's comment. The real usefulness of this code is that it can (or at least should) be written *very* carefully so that it will run on, say, MATLAB 3. Assuming that has been done, that makes this code useful for those of us who no longer have a copy of MATLAB 3.
Getting these 2 lines right is *not* a trivial thing, as Sergei claims it to be.
Will the proposed 'simpler' solution from Jos work for the first version of Matlab I used ... ???
1) There are infinite number of simple useful two-liners that can be created in Matlab. Add to that an equal number of three and four-liners and you will see how FEX becomes a huge pile of ?simple but useful? junk.
2) Anonymous comments ought to be ignored by default. Serious participants have no reason to hide its face. We are not dealing drugs here.
Both comments are true, but the methods suggested do not return a real number as this code does. They will return a character string.
If you want to make code that works for all versions prior to a particular release, that is where I see the value in this code. Plus you don't have to remember how to do it, just the code.
It is simple, only two lines, but useful.
Too simple functionality to place here (and can be done simpler).
vernum = ver('matlab') ;
vernum = str2double(vernum.Version) ;
would do this better.
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!