Matlab version specific code

From OPS
Jump to: navigation, search

All CReSIS Toolbox Development Guides

This page discusses methods to write code to handle different versions of matlab.

Contents

Figure Handles

Figure handles in old versions of Matlab were stored as double scalars. Since Matlab 2014a or 2014b, figure handles have been stored as objects. If you need to access the figure number, use code like this:

if isa(h_fig,'double')
  h_fig
else
  h_fig.Number
end

Number of Arguments Check

See get_filenames.m

matlab_ver = ver('matlab');
if str2double(matlab_ver.Version) >= 9.0
  narginchk(4,5);
else
  error(nargchk(4,5,nargin,'struct'));
end

Function Dependencies

See torque_compile.m for example.

matlab_ver = ver('matlab');
% 8.3 was first version of builtin, but it has bugs. So using 8.6 as
% threshold especially since fdep no longer works in that version
use_builtin_fdep = str2double(matlab_ver.Version) >= 8.6;

...

        if use_builtin_fdep
          flist = matlab.codetools.requiredFilesAndProducts(fun);
        else
          warning off
          flist = fdep(fun,'-q');
          warning on
          flist = flist.fun;
        end

Name Variable in Context Menu Callback

See imb.prefwin.sourceLBcallback.m

% Turn object into struct so we can check existence of fields
h_status = get(status);

% Determine the name of the field that contains the selected menu item
% 9.3 was first??? version using "Text" instead of "Label"
if 0
  matlab_ver = ver('matlab');
  use_text = str2double(matlab_ver.Version) >= 9.3;
elseif 1
  if isfield(h_status,'Text')
    use_text = 1;
  else
    use_text = 0;
  end
end
Personal tools
Namespaces

Variants
Actions
Navigation
Tools