# MATLAB Programming/Differences between Octave and MATLAB

Octave has been mainly built with MATLAB compatibility in mind. It has 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."

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 give an error when certain Octave-only syntax is used.

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.

## Contents

- 1 C-Style Autoincrement and Assignment operators
- 2 Temporaries
- 3 Product of booleans
- 4 nargin
- 5 startup.m
- 6 ['abc ';'abc']
- 7 Calling Shells
- 8 Attempting to load empty files
- 9 fprintf and printf
- 10 Whitespace
- 11 Line continuation
- 12 Logical operator NOT
- 13 GNU Octave Control Package
- 14 Some other differences
- 15 Notes about specific functions
- 16 References
- 17 See also

## C-Style Autoincrement and Assignment operators[edit]

Octave supports C-style autoincrement and assignment operators:

i++; ++i; i+=1; etc.

MatLab does not.

## Temporaries[edit]

Octave and Matlab support temporary expressions.

```
tmp = size(mtx);
columns = tmp(2); % works in both
columns = size(mtx)(2); % works in Octave, fails in Matlab
columns = size(mtx,2); % works in both
```

## Product of booleans[edit]

Matlab (R2011b) and Octave (3.6.4) responds differently when computing the product of boolean values:

X = ones(2,2) ; prod(size(X)==1) Matlab: PROD is only supported for floating point input. Octave: ans = 0

## nargin[edit]

Nargin returns the number of input arguments of a function. Matlab (R2011b) will not allow the following; Octave will.

function myfun = testfun(c) if (nargin == 1) nargin = 2; else nargin = 3 end

## startup.m[edit]

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' which can be edited to execute existing files. This means that '.octaverc' can be edited to look for and execute a 'startup.m' file.

if ( exist ('startup.m', 'file') ) source ('startup.m') # load startup.m like matlab endif

## ['abc ';'abc'][edit]

['abc ';'abc'] is allowed in Octave; Matlab returns: ?? Error using ==> vertcat

In Octave the result will be a 2 by 3 matrix.

## Calling Shells[edit]

the "! STRING" syntax calls a shell with command STRING in Matlab. Octave does not recognize ! as system call, since it is used in logical operations. Always use 'system (STRING)' for compatibility.

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 '.octaverc' file:

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

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

## Attempting to load empty files[edit]

MATLAB lets you load empty files, OCTAVE does not.

system('touch emptyfile') ; A = load('emptyfile')

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

## fprintf and printf[edit]

Matlab doesn't support `printf' as a command for printing to the screen.

foo = 5; printf ('My result is: %d\n', foo) % prints to STDOUT. Octave only

If using Matlab, `fprintf' covers writing both to the screen and to a file by omitting the optional file-handle argument:

foo = 5; fprintf('My result is: %d\n', foo) % prints to STDOUT, Octave and Matlab

## Whitespace[edit]

Matlab does not allow whitespace before the transpose operator but Octave does (it is just an operator like others).

[0 1]' % works in Matlab and Octave [0 1] ' % works only in Octave

## Line continuation[edit]

Matlab always requires `...' for line continuation.

rand (1, ... 2)

while Octave *also* supports

rand (1, 2)

and

rand (1, \ 2)

## Logical operator NOT[edit]

Octave allows users to use both ~ and ! with boolean values. The first is for matlab compatibility, while ! will be more familiar to C/Java/etc programmers. If you use the latter, however, you'll be writing code that Matlab will not accept:

- For not-equal comparison, Octave can use both '~=' or '!='. Matlab requires '~='.

## GNU Octave Control Package[edit]

Both MATLAB and Octave have toolboxes intended to control system design. In Octave, the toolbox is called the Octave Control Package. The package can be downloaded, compiled and installed with the command `pkg install control`from the Octave prompt. Users of Debian and its derivatives can install it by installing the package "octave-control", if it is not installed by default.

For more information about functions' syntax, type `help <name of function>`. For more information about the Control Package, view the PDF manual in the package's "doc" folder.

## Some other differences[edit]

- MATLAB uses the percent sign '%' to begin a comment. Octave uses both the hash symbol '#' and the percent sign '%' interchangeably.
- For exponentiation, Octave can use `^' or `**'; Matlab requires `^'.
- For string delimiters, Octave can use ' or "; Matlab requires '.
- To end blocks, Octave can use `end' or specify the block with `end{if,for, ...}'; Matlab requires `end'.
- Octave supports C-style hexadecimal notation (e.g. "0xF0"); Matlab requires the
*hex2dec*function (e.g. "hex2dec('F0')"). - If something (like Netlab) need a function called fcnchk, you can just put the following into a file called fcnchk.m and put it somewhere Octave can find it:

function f=fcnchk(x, n) f = x; end

- The main difference is the lack of GUI for Octave. There have been many attempts to solve this issue though they are all eventually abandoned once they no longer work with new Octave versions. There is an experimental GUI as part of Octave already released, which is planned to become official with the next release.

## Notes about specific functions[edit]

- For "dbstep, in" use "dbstep"; for "dbstep", use "dbnext"
- For "eig(A,B)" use "qz(A,B)"
- fputs function is not available in MATLAB. Use fprintf instead.
- page_output_immediately = 1 should this be default in --traditional?
- strread and textscan in Octave 3.4.0 are not fully compatible with their implementations in Matlab 2009b (and probably later versions as well). For instance, the N=-1 option (repeat reading format until end of string) is not implemented in Octave 3.4.0 . Using a value of N=a positive integer (read format N times) does work the same as in Matlab.
- textscan function is not included in Octave versions prior to 3.4.0. Use fscanf instead.
- For the linprog function, MATLAB is more permissive by allowing the "a" and "b" inputs to be either row or column vectors. Octave requires that they be column vectors.
- In Octave, one can specify data labels (or legends) with the plot function, while in Matlab, one can only use the legend function.

Octave: plot(x, y, ';label;') Matlab/Octave: plot(x, y); legend('label')

- The error(msg) function in Matlab is a no-op if the message is empty. In Octave, it results in an error.