0% found this document useful (0 votes)
500 views

MATLAB Programming Differences Between Octave and MATLAB

This is a document that describes the most important differences and similarities between MATLAB and GNU/Octave and the advantages and disadvantages of each one, although must programs should work on both patforms
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
500 views

MATLAB Programming Differences Between Octave and MATLAB

This is a document that describes the most important differences and similarities between MATLAB and GNU/Octave and the advantages and disadvantages of each one, although must programs should work on both patforms
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MATLAB Programming/Dierences between

Octave and MATLAB


Octave has been mainly built with MATLAB compati- 2 Temporaries
bility in mind. It has a lot of features in common with
MATLAB:
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

1. Matrices as fundamental data type.


2. Built-in support for complex numbers.
3. Powerful built-in math functions and extensive function libraries.

3 Product of booleans

4. Extensibility in the form of user-dened functions.

MATLAB (R2011b) and Octave (3.6.4) responds dierSome of the dierences that do exist between Octave and
ently when computing the product of boolean values:
MATLAB can be worked around using user preference
X = ones(2,2) ; prod(size(X)==1) MATLAB: PROD is
variables.
only supported for oating point input. Octave: ans = 0
GNU Octave is mostly compatible with MATLAB. However, Octaves parser allows some (often very useful) syntax that MATLABs does not, so programs written for
Octave might not run in MATLAB. For example, Octave 4 nargin
supports the use of both single and double quotes. MATLAB only supports single quotes, which means parsing Nargin returns the number of input arguments of a funcerrors will occur if you try to use double quotes (e.g. in an tion. MATLAB (R2011b) will not allow the following;
Octave script when run on MATLAB). Octave and MAT- Octave will.
LAB users who must collaborate with each other need to
function myfun = testfun(c) if (nargin == 1) nargin = 2;
take note of these issues and program accordingly.
else nargin = 3 end
Note: Octave can be run in traditional mode
(by including the --traditional ag when starting Octave) which makes it give an error when
certain Octave-only syntax is used.

5 startup.m
MATLAB will execute a le named 'startup.m' in the directory it was called from on the command line. Octave
does not. It will, however, execute a le named '.octaverc'
which can be edited to execute existing les. This means
that '.octaverc' can be edited to look for and execute a
'startup.m' le.

This chapter documents instances where MATLABs


parser will fail to run code that will run in Octave, and
instances where Octaves parser will fail to run code that
will run in MATLAB. This page also contains notes on
dierences between things that are dierent between Octave (in traditional mode) and MATLAB.

if ( exist ('startup.m', 'le') ) source ('startup.m') # load


startup.m like MATLAB endif

C-Style Autoincrement and Assignment operators


6 ['abc ';'abc']

Octave supports C-style autoincrement and assignment


operators:
['abc ';'abc'] is allowed in Octave; MATLAB returns: ??
i++; ++i; i+=1; etc.
Error using ==> vertcat
MatLab does not.

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


1

14 SOME OTHER DIFFERENCES

Calling Shells

12 Logical operator NOT

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.

Octave allows users to use both ~ and ! with boolean


values. The rst 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
If you really miss the one-character shortcut, for con- MATLAB will not accept:
venience on the command line you can create a similar
For not-equal comparison, Octave can use both '~='
shortcut by dening the following in your '.octaverc' le:
or '!='. MATLAB requires '~='.
function S(a), system(a); end
Now S STRING will evaluate the string in the shell.

13 GNU Octave Control Package


8

Attempting to load empty les

Both MATLAB and Octave have toolboxes intended to


control system design. In Octave, the toolbox is called
MATLAB lets you load empty les, OCTAVE does not. the Octave Control Package. The package can be downsystem('touch emptyle'); A = load('emptyle') MAT- loaded, compiled and installed with the command pkg inLAB R2011b : A=[] Octave 3.6.2 : error: load: le `emp- stall control from the Octave prompt. Users of Debian
tyle' seems to be empty! error: load: unable to extract and its derivatives can install it by installing the package
octave-control, if it is not installed by default.
matrix size from le `emptyle'

fprintf and printf

For more information about functions syntax, type help


<name of function>. For more information about the
Control Package, view the PDF manual in the packages
doc folder.

Octave supports both printf and fprintf as a command for Small dierences exist - an example is c2d. Here are the
printing to the screen. MATLAB requires fprintf:
two formats for the bilinear transformation with an analog
foo = 5; printf ('My result is: %d\n', foo) % Prints to model C:
STDOUT. Octave only
discrete = c2d(C,0.5,'tustin'); % Matlab
fprintf covers writing both to the screen and to a le by
omitting the optional le-handle argument:

discrete = c2d(C,0.5,'bi'); % GNU Octave

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


STDOUT. Octave and MATLAB

14 Some other dierences


10

Whitespace

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

11

Line continuation

MATLAB always requires ... for line continuation.


rand (1, ... 2)
while Octave also supports
rand (1, 2)
and
rand (1, \ 2)

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 endif, endfor, ...; 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 le called
fcnchk.m and put it somewhere Octave can nd it:

3
function f=fcnchk(x, n) f = x; end
The main dierence 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 ocial with the
next release.

15

Notes about specic functions

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.

16

References

http://wiki.octave.org/FAQ#How_is_Octave_
different_from_Matlab.3F

17

See also

Octave Programming Tutorial

18

18
18.1

TEXT AND IMAGE SOURCES, CONTRIBUTORS, AND LICENSES

Text and image sources, contributors, and licenses


Text

MATLAB Programming/Dierences between Octave and MATLAB Source: https://en.wikibooks.org/wiki/MATLAB_Programming/


Differences_between_Octave_and_MATLAB?oldid=2772831 Contributors: Npettiaux, Hankwang, Mattb112885, Albmont, Billymac00,
Mcld, Recent Runes, SQL, Waldir, Hauberg~enwikibooks, Xmjiao, , Adrignola, Sargas~enwikibooks, Gryllida, Angry bee,
Wyverald, Arthornsby, Adamcrume, Carandraug, Mhadi.afrasiabi, Hofmic, Decatur-en, Filipeataide and Anonymous: 33

18.2

Images

18.3

Content license

Creative Commons Attribution-Share Alike 3.0

You might also like