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!
- For compatablility, it is best to specify absolute paths of files for LOAD. Matlab (7.0) vs Octave (2.1.71): paths are not searched for .mat files in the same way as .m files:
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'
- Matlab (7.0) will not allow the following (was a typo); Octave (2.1.71) will.
X = ones(2,2) ; prod(size(X)==1) Matlab: ??? Function 'prod' is not defined for values of class 'logical'. Octave: ans = 0
- Matlab (7.0) will not allow the following; Octave (2.1.71) will.
function a = testfun(c)
if (nargin == 1)
nargin = 2;
end
- Matlab will execute a file named startup.m in the directory it was called from on the command line. Octave does not.
- Tip for sharing .mat files between Octave and Matlab: always use save -V6 if you are using Matlab 7.X. Octave version 2.1.x cannot read Matlab 7.X .mat files. Octave 2.9.x can read Matlab 7.X .mat files.
- ['abc ';'abc'] is allowed in Octave in --traditional mode; Matlab rerturns: ?? Error using ==> vertcat
- ! STRING calls a shell with command STRING in Matlab. Octave does not recognize !. Always use system(STRING) for compatability.
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.
- a = ['\n'] ; b=find('abc' == '\n')
Matlab 6.5 : ??? Error using ==> eq Array dimensions must match for binary array op.
Octave 2.1.71 : b=[]
- hist.m in Octave has a normalization input, Matlab does not.
- Octave (2.1.7X) uses "OCTAVE_VERSION", Matlab uses "ver" for version informaton. Octave 2.9.5 has "ver"
- Cell arrays and structures print to screen in different format. There may be switches to make them ths same, for example, struct_levels_to_print. None are set in --traditional mode, however.
- There used to be a difference in datestr between Octave-forge and Matlab. Has this been changed?
- load in Matlab = load --force in Octave (should --force be default in --traditional?)
- page_output_immediately = 1 should this be default in --traditional?
- system('touch emptyfile') ; A = load('emptyfile')
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'
- Matlab doesn't support `printf'.
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.
- Matlab doesn't allow whitespace before the transpose operator.
[0 1]'
works in Matlab, but
[0 1] '
doesn't. Octave properly parses both cases.
- Matlab always requires `...'.
rand (1, ...
2)
while both
rand (1,
2)
and
rand (1, \
2)
work in Octave in addition to `...'. Matlab chokes on the latter two.
- For a logical-and, Octave can use `&' or `&&'; Matlab requires `&'.
- For a logical-or, Octave can use `|' or `||'; Matlab requires `|'. (note: Octave's '||' and '&&' return a scalar, '|' and '&' return matrices)
- For exponentiation, Octave can use `^' or `**'; Matlab requires `^'.
- For string delimiters, Octave can use ` or "; Matlab requires '.
- For ends, Octave can use `end{if,for, ...}'; Matlab requires `end'.
- For "dbstep in" use "dbstep"; for "dbstep" use "dbnext"
- For "eig(A,B)" use "qz(A,B)"
- For gallery, compan, and hadamard install http://www.ma.man.ac.uk/~higham/testmat.html
- For subspace try http://www.mathworks.com/support/ftp/linalgv5.shtml
- For datetick use gnuplot commands:
__gnuplot_set xdata time __gnuplot_set timefmt "%d/%m" __gnuplot_set format x "%b %d"