spm_check_version.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. function varargout = spm_check_version(tbx,chk)
  2. % Check a version number against a Toolbox version
  3. %
  4. % FORMAT tbx = spm_check_version
  5. % tbx - toolbox name {'matlab','octave',...}
  6. %
  7. % FORMAT v = spm_check_version(tbx)
  8. % tbx - toolbox name {'matlab','octave','signal',...}
  9. %
  10. % v - version number {string}
  11. %
  12. % FORMAT status = spm_check_version(tbx,chk)
  13. % tbx - toolbox name {'matlab','octave','signal',...}
  14. % chk - version number to be checked {string}
  15. %
  16. % status - outcome of the comparison:
  17. % -1: Toolbox version is earlier than the user supplied version
  18. % 0: Toolbox and user versions are the same
  19. % 1: Toolbox version is later than the user supplied version
  20. % Think of it this way, the sign of status is determined by
  21. % MATLAB_TOOLBOX_VERSION - USER_VERSION (i.e., THE VERSION YOU
  22. % INPUT).
  23. %__________________________________________________________________________
  24. %
  25. % This function checks if a user supplied version number is less than,
  26. % equal to or greater than the version number of specified toolbox. If no
  27. % toolbox is specified the function checks the version of MATLAB. User
  28. % defined toolboxes can be checked but the Contents.m file must conform
  29. % to the specification defined in ver.m
  30. %
  31. % This function assumes that the version number is really a text string
  32. % with fields major.minor.revision.build. Other formats are not supported.
  33. % Checking is done to the level specified by the input version. Thus an
  34. % input of '7' will be rated as the same version as 7.1, but 7.0 would be
  35. % rated as earlier than 7.1.
  36. %__________________________________________________________________________
  37. %
  38. % EXAMPLES:
  39. %
  40. % If the MATLAB version is 7.1.0.83, and the user supplied version is '7':
  41. % status = spm_check_version('matlab','7');
  42. % returns status == 0 : major revision numbers are the same.
  43. %
  44. % If the MATLAB version is 7.1.0.0, and the user supplied version is '7.1':
  45. % status = spm_check_version('matlab','7');
  46. % returns status == 0 : major and minor revision numbers are the same.
  47. %
  48. % If the MATLAB version is 7.1.0.83, and the user supplied version is '7.2':
  49. % status = spm_check_version('matlab','7.2');
  50. % returns status == -1 : major + minor revision is earlier for MATLAB.
  51. %
  52. % If the MATLAB version is 6.5.1, and the user supplied version is '6.5.0'.
  53. % status = spm_check_version('matlab','6.5.0');
  54. % returns status == 1 : major + minor + release revision is later
  55. % for MATLAB
  56. % The statement ( spm_check_version('matlab','6.5.0') > 0 ) is true for
  57. % all MATLAB Toolbox versions after 6.5.0.
  58. %__________________________________________________________________________
  59. %
  60. % See also VERSION, VER, VERLESSTHAN.
  61. %__________________________________________________________________________
  62. % Copyright (C) 2006-2014 Wellcome Trust Centre for Neuroimaging
  63. % Darren Gitelman
  64. % $Id: spm_check_version.m 6156 2014-09-05 17:34:53Z guillaume $
  65. %-Detect software used
  66. %==========================================================================
  67. if ~nargin || isempty(tbx)
  68. if exist('OCTAVE_VERSION','builtin')
  69. tbx = 'octave';
  70. else
  71. tbx = 'matlab';
  72. end
  73. if ~nargin, varargout = {tbx}; return; end
  74. end
  75. %-Get the requested toolbox version
  76. %==========================================================================
  77. if strcmpi(tbx,'matlab')
  78. % MATLAB is a special case. The ver command does not report the
  79. % entire version string.
  80. tbxVer = strtok(builtin('version'));
  81. elseif strcmpi(tbx,'octave')
  82. tbxVer = version;
  83. tbxVer = strrep(tbxVer,'+','');
  84. else
  85. tbxStruct = ver(tbx);
  86. if isempty(tbxStruct)
  87. error('Cannot find given toolbox.');
  88. elseif numel(tbxStruct) > 1
  89. error('Too many toolboxes found for given toolbox name.')
  90. end
  91. tbxVer = tbxStruct.Version;
  92. end
  93. if nargin == 1, varargout = {tbxVer}; return; end
  94. %-Parse user supplied version number
  95. %==========================================================================
  96. if strcmpi(tbx,'matlab') && strcmpi(spm_check_version,'octave')
  97. varargout = {1}; % hack
  98. return;
  99. end
  100. % If a number is supplied then convert to text
  101. %--------------------------------------------------------------------------
  102. if isnumeric(chk)
  103. chk = num2str(chk);
  104. end
  105. % If too many fields in input then error
  106. %--------------------------------------------------------------------------
  107. if numel(strfind(chk,'.')) > 3
  108. error(['Input string has too many fields. ',...
  109. 'Only major.minor.release.build fields are supported.']);
  110. end
  111. % parse the input string into a cell array of strings
  112. %--------------------------------------------------------------------------
  113. vCHK = textscan(chk,'%s %s %s %s','delimiter','.');
  114. % find empty cells. These will be set to 0 in both cell arrays so that the
  115. % same revision fields are compared
  116. %--------------------------------------------------------------------------
  117. emptyCHK = cellfun('isempty',vCHK);
  118. % parse the requested toolbox version string into a cell array of strings.
  119. %--------------------------------------------------------------------------
  120. vTBX = textscan(tbxVer,'%s %s %s %s','delimiter','.');
  121. % find empty cells. These will be removed from the cell array so that the
  122. % same revision fields are compared
  123. %--------------------------------------------------------------------------
  124. emptyTBX = cellfun('isempty',vTBX);
  125. % combine empty cell indices
  126. %--------------------------------------------------------------------------
  127. emptyCells = find(emptyCHK | emptyTBX);
  128. % and remove them.
  129. %--------------------------------------------------------------------------
  130. vCHK(emptyCells) = [];
  131. vTBX(emptyCells) = [];
  132. % final version fields to compare are converted to numbers
  133. %--------------------------------------------------------------------------
  134. vCHKMat = str2num(strvcat([vCHK{:}]))';
  135. vTBXMat = str2num(strvcat([vTBX{:}]))';
  136. %-Compare versions
  137. %==========================================================================
  138. % This array will be used to decide which version is the later one. The
  139. % differences between versions are computed on each element of the version
  140. % number: major.minor.release.build and then multiplied by the base2array
  141. % and summed. This is equivalent to logically combining the base 2 place
  142. % values and ensures that the fields are evaluated by their significance.
  143. % The sign of the summed values gives the result. Positive values mean the
  144. % toolbox version is later, negative values mean the user supplied version,
  145. % is later, and 0 means they are equal.
  146. %--------------------------------------------------------------------------
  147. base2array = [8 4 2 1];
  148. base2array(emptyCells) = [];
  149. % take the difference between version elements.
  150. %--------------------------------------------------------------------------
  151. vDiff = vTBXMat - vCHKMat;
  152. % get the sign of the differences
  153. %--------------------------------------------------------------------------
  154. signDiff = sign(vDiff);
  155. % multiply by the base2array
  156. %--------------------------------------------------------------------------
  157. vVal = signDiff.*base2array;
  158. % If the sign of the sum is positive then toolbox version is later; if 0
  159. % they are the same, and if negative the user supplied version is later.
  160. % Think of it this way: MATLAB_TOOLBOX_VERSION - USER_VERSION.
  161. % Remember the comparison is made with respect to the FIELDS SUPPLED IN THE
  162. % INPUT
  163. %--------------------------------------------------------------------------
  164. switch sign(sum(vVal))
  165. case -1 % MATLB_TOOLBOX_VERSION is EARLIER than USER_VERSION
  166. status = -1;
  167. case 0 % MATLB_TOOLBOX_VERSION = EQUALS = USER_VERSION
  168. status = 0;
  169. case 1 % MATLB_TOOLBOX_VERSION is LATER than USER_VERSION
  170. status = 1;
  171. end
  172. varargout = {status};