MATLAB Programming/Differences between Octave and MATLAB
From Wikibooks, the open-content textbooks collection
Chapter 1: A Tutorial Introduction
Chapter 2: Basic MATLAB Concepts
Chapter 3: Data Storage and Manipulation
Data Types and Operations on Point Values
Arrays and Matrices
- What is an array?
- Introduction to array operations
- Vectors and basic vector operations
- Struct Arrays
- Cell Arrays
- Sparce Matrices
Chapter 4: Graphics
Chapter 5: M File Programming
- Scripts
- Comments
- The Input Function
- Control Flow
- Loops and Branches
- Error Messages
- MATLAB Caveats
- Debugging M Files
Chapter 6: Mathematical Manipulations
Linear Algebra
Differential Equations
Chapter 7: More advanced I/O
Chapter 8: Examples
Chapter 9: Object-Oriented Programming
Chapter 10: An alternative to MATLAB: Octave
- What is Octave ?
- Octave/MATLAB differences
Chapter 11: Toolboxes
Octave has been mainly built with MATLAB compatibility in mind. It essentially shares a lot of features in common with MATLAB:
- Matrices as fundamental data type.
- Built-in support for complex numbers.
- Powerful built-in math functions and extensive function libraries.
- Extensibility in the form of user-defined functions.
Some of the differences that do exist between Octave and MATLAB can be worked around using "user preference variables."[1]
GNU Octave is mostly compatible with Matlab. However, Octave's parser allows some (often very useful) syntax that Matlab's does not, 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, which means parsing errors will occur if you try to use double quotes (e.g. in an Octave script when run on Matlab). Octave and Matlab users who must collaborate with each other need to take note of these issues and program accordingly.
- Note: Octave can be run in "traditional mode" (by including the --traditional flag when starting Octave) which makes it behave in a slightly more Matlab-compatible way.
This chapter documents instances where Matlab's parser will fail to run code that will run in Octave, and instances where Octave's parser will fail to run code that will run in Matlab. This page also contains notes on differences between things that are different between Octave (in traditional mode) and Matlab.
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.
[edit] load
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'
For any other purpose, don't use absolute paths. It is bad programming style. Don't do it. It causes many problems.
[edit] prod
Matlab (7.0) and Octave (2.1.71) respond differently to the following (which was a typo - it's meaningless to ask for the prod of a boolean value):
X = ones(2,2) ; prod(size(X)==1) Matlab: ??? Function 'prod' is not defined for values of class 'logical'. Octave: ans = 0
[edit] nargin
Matlab (7.0) will not allow the following; Octave (2.1.71) will.
function a = testfun(c) if (nargin == 1) nargin = 2; end
[edit] startup.m
Matlab will execute a file named startup.m in the directory it was called from on the command line. Octave does not. It will, however, execute a file named .octaverc.
[edit] ['abc ';'abc']
['abc ';'abc'] is allowed in Octave in --traditional mode; Matlab rerturns: ?? Error using ==> vertcat
[edit] Calling Shells
the "! STRING" syntax calls a shell with command STRING in Matlab. Octave does not recognize !. Always use system(STRING) for compatability.
If you really miss the one-character shortcut, for convenience on the command line you can create a similar shortcut by defining 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.
[edit] String comparisons
In MATLAB you must use special comparison functions for strings, rather than standard comparison functions, because the strings are treated as arrays of characters and unlike other arrays, they are often of significantly varying length. OCTAVE automatically adjusts to this without use of special functions. For example, you can do this in OCTAVE but not in MATLAB.
b=find('abc' == '\n')
Matlab 6.5 : ??? Error using ==> eq Array dimensions must match for binary array op. Octave 2.1.71 : b=[]
[edit] hist
hist.m in Octave has a normalization input, Matlab does not.
[edit] Getting Version Information
Octave (2.1.7X) uses "OCTAVE_VERSION", Matlab uses "ver" for version informaton. (Octave 2.9.5 and later has "ver", so is compatible in this sense.)
[edit] Format of printing to screen
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.
[edit] Attempting to load empty files
MATLAB lets you load empty files, OCTAVE does not.
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'
[edit] fprintf and printf
Matlab doesn't support `printf' as a command for printing to the screen.
foo = 5;
printf('My result is: %d\n', foo)
works in Octave, but not Matlab. If using Matlab, `fprintf' covers writing both to the screen and to a file:
foo = 5;
fprintf('My result is: %d\n', foo)
In Matlab, the omission of the optional file-handle argument to fprintf (or using the special value 1 for standard output or 2 for standard error) causes the text to be printed to the screen rather than to a file.
[edit] Whitespace
Matlab doesn't allow whitespace before the transpose operator.
[0 1]'
works in Matlab, but
[0 1] '
doesn't. Octave allows both cases.
[edit] Line continuation
Matlab always requires `...' for line continuation.
rand (1, ...
2)
while both
rand (1,
2)
and
rand (1, \
2)
work in Octave, in addition to `...'.
[edit] Subtracting the mean
Matlab understands the intuitive concept of subtracting the mean from each row/column, even though the result of the "mean" function gives a differently-shaped matrix:
x = z - mean(z,2)
In Octave (v2.9.9 tested) you must make more explicit how the matrices should be combined, by repeating the "mean" matrix appropriately:
x = z - repmat(mean(z,2), 1, size(z,2))
This specific example is also covered in both Matlab and Octave by the "center" function.
[edit] Some other differences
- There used to be a difference in datestr between Octave-forge and Matlab. Has this been changed?
- MATLAB uses the percent sign to begin a comment. Octave uses both the pound sign and the percent sign interchangeably.
- MATLAB has no fputs function. Call fprintf instead.
- load in Matlab = load --force in Octave (should --force be default in --traditional?)
- page_output_immediately = 1 should this be default in --traditional?
- 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"

