Octave Wiki | RecentChanges

MatlabOctaveCompatibility

Matlab/Octave Compatibility

GNU Octave is mostly compatible with Matlab. However, Octave's parser goes above and beyond Matlab's, so programs written for Octave might not run in Matlab. For example, Octave supports the use of both single and double quotes. Matlab only supports single quotes and double quotes result in parsing errors. Octave users who must collaborate with Matlab users need to take note of these (annoying) issues and program according.

This page attempts to document where Matlab's parser fails to run perfectly proper Octave. This page also contains notes on differences between things that are different between Octave (in --traditional mode) and Matlab. Please add to this page and make corrections!

  Matlab: a = 1; save /tmp/a.mat a ; addpath('/tmp'); load a.mat % OK
  Octave: a = 1; save /tmp/a.mat a ; LOADPATH=['/tmp:',LOADPATH]; ; load a.mat % error: load: nonexistent file: `a.mat'
 

   X = ones(2,2) ; prod(size(X)==1) 
 
   Matlab: ??? Function 'prod' is not defined for values of class 'logical'.
   Octave: ans = 0

  function a = testfun(c)
   if (nargin == 1)
    nargin = 2;
   end

For convenience on the command line you can define the following in your 2.9.x octave startup file:

   function S(a), system(a); end
   mark_as_rawcommand('S')

Now "S STRING" will evaluate the string in the shell.

    Matlab 6.5    : ??? Error using ==> eq Array dimensions must match for binary array op.
    Octave 2.1.71 : b=[]

   Matlab 6.5    : A=[]
   Octave 2.1.71 : error: load: file `emptyfile' seems to be empty!
                   error: load: unable to extract matrix size from file `emptyfile'

    foo = 5;
    printf('My result is: %d\n', foo)

works in Octave, but not Matlab. If using Matlab, the kludge is to issue `fprintf' instead:

    foo = 5;
    fprintf('My result is: %d\n', foo)

This is a kludge since the command is printing to the screen, not to a file. Octave's `fprintf' is equivalent to `printf' if the argument is simply a character string.

    [0 1]'

works in Matlab, but

    [0 1] '

doesn't. Octave properly parses both cases.

    rand (1, ...
          2)

while both

    rand (1,
          2)

and

    rand (1, \
          2)

work in Octave in addition to `...'. Matlab chokes on the latter two.

  __gnuplot_set xdata time 
  __gnuplot_set timefmt "%d/%m" 
  __gnuplot_set format x "%b %d"