Overloading functions in Matlab and GNU Octave

Matlab and GNU Octave are constantly adding new functionality. However, legacy versions remain in use for years. One in general may not be able to overload a Matlab function in Octave because of how Octave provides many functions as files, and cannot distinguish them from user files.

The “overloading” we do here is to provide a similarly but distinctively named function that seamlessly uses the new syntax when available, with a simple fallback. In this isfile example the fallback only works for single files.

Create a file “is_file.m” containing:

function ret = is_file(path)

if exist('isfile', 'builtin') == 5 || exist('isfile', 'file') == 2
  ret = isfile(path);
  ret = exist(path, 'file') == 2;

end % function