MATLAB_Programming_Differences_between_Octave_and_MATLAB
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:
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, whereas older versions of MATLAB only supported
single quotes, which meant parsing errors occurred if you tried to use double quotes (e.g. in an Octave script
when run on MATLAB). More recent versions of MATLAB introduced double quotes, but with different
functionality to single quotes (albeit with some overlap in functionality). 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.
Product of booleans
MATLAB (R2011b) and Octave (3.6.4) respond 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
They both produce the same result (ans=0) in MATLAB (R2015a) and above
nargin
Nargin returns the number of input arguments of a function. MATLAB (R2011b) will not allow the following;
Octave will.
startup.m
MATLAB will execute a file named 'startup.m' in the directory it was called from on the command line. Old
versions of Octave do not. Starting with Octave 4.2.0 it behaves the same as Matlab. For older versions of
Octave, it will execute a file named '.octaverc' which can be edited to execute existing startup files. This means
that '.octaverc' can be edited to look for and execute a 'startup.m' file.
In MATLAB:
['abc ';'abc']
['abc ';'abc'] is now allowed in both Octave and MATLAB; (MATLAB previously would return: ?? Error
using ==> vertcat)
Calling Shells
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:
system('touch emptyfile');
A = load('emptyfile')
foo = 5;
printf ('My result is: %d\n', foo) % Prints to STDOUT. Octave only
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
MATLAB does not allow whitespace before the transpose operator but Octave does (it is just an operator like
others).
rand (1,
2)
For both programs, a line break without '...' within an array shifts to the next row
>> R=[1 2 3
7 8 9]
R =
1 2 3
7 8 9
Assignment
Octave supports
z = y = x + 3
MATLAB requires
y = x + 3
z = y
Octave allows
global isOctave;
isOctave = (exist('OCTAVE_VERSION') > 0);
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.
Small differences exist - an example is c2d. Here are the two formats for the bilinear transformation with an
analog model C:
function f=fcnchk(x, n)
f = x;
end
The main difference used to be the lack of GUI (http://wiki.octave.org/FAQ#GUI) for Octave.
With version 4.0 Octave has a GUI as its default interface.
The error(msg) function in MATLAB is a no-op if the message is empty. In Octave, it results in
an error.
The contains() function is not available in Octave. Use ~isempty(strfind()) instead.
References
Octave FAQ: Differences between Octave and Matlab (http://wiki.octave.org/FAQ#Differences_
between_Octave_and_Matlab)
Octave FAQ: What features are unique to Octave? (http://wiki.octave.org/FAQ#What_features_a
re_unique_to_Octave.3F)
See also
Octave Programming Tutorial
Text is available under the Creative Commons Attribution-ShareAlike License.; additional terms may apply. By using this
site, you agree to the Terms of Use and Privacy Policy.