matlab programming differences between octave and matlab
TRANSCRIPT
-
7/23/2019 MATLAB Programming Differences Between Octave and MATLAB
1/4
MATLAB Programming/Differences betweenOctave and MATLAB
Octave has been mainly built with MATLAB compati-
bility in mind. It has a lot of features in common with
MATLAB:
1. Matrices as fundamental data type.
2. Built-in support for complex numbers.
3. Powerful built-in math functions and extensive func-
tion libraries.
4. 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. How-
ever, Octaves parser allows some (often very useful) syn-
tax that MATLABs does not, so programs written for
Octave might not run in MATLAB. For example, Octave
supports the use of both single and double quotes. MAT-
LAB only supports single quotes, which means parsingerrors will occur if you try to use double quotes (e.g. in an
Octave script when run on MATLAB). Octave and MAT-
LAB 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 start-
ing Octave) which makes it give an error when
certain Octave-only syntax is used.
This chapter documents instances where MATLABs
parser will fail to run code that will run in Octave, andinstances where Octaves parser will fail to run code that
will run in MATLAB. This page also contains notes on
differences between things that are different between Oc-
tave (in traditional mode) and MATLAB.
1 C-Style Autoincrement and As-
signment operators
Octave supports C-style autoincrement and assignment
operators:
i++; ++i; i+=1; etc.
MatLab does not.
2 Temporaries
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
3 Product of booleans
MATLAB (R2011b) and Octave (3.6.4) responds differ-
ently 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
4 nargin
Nargin returns the number of input arguments of a func-tion. MATLAB (R2011b) will not allow the following;
Octave will.
function myfun = testfun(c) if (nargin == 1) nargin = 2;
else nargin = 3 end
5 startup.m
MATLAB will execute a file named 'startup.m' in the di-
rectory 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
6 ['abc ';'abc']
['abc ';'abc'] is allowed in Octave; MATLAB returns: ??Error using ==> vertcat
In Octave the result will be a 2 by 4 matrix.
1
-
7/23/2019 MATLAB Programming Differences Between Octave and MATLAB
2/4
2 14 SOME OTHER DIFFERENCES
7 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 con-
venience 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.
8 Attempting to load empty files
MATLAB lets you load empty files, OCTAVE does not.
system('touch emptyfile'); A = load('emptyfile') MAT-LAB R2011b : A=[] Octave 3.6.2 : error: load: file `emp-
tyfile' seems to be empty! error: load: unable to extract
matrix size from file `emptyfile'
9 fprintf and printf
Octave supports both printf and fprintf as a command for
printing to the screen. MATLAB requires fprintf:
foo = 5; printf ('My result is: %d\n', foo) % Prints to
STDOUT. Octave onlyfprintf 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
10 Whitespace
MATLAB does not allow whitespace before the trans-
pose 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 Octavealsosupports
rand (1, 2)
and
rand (1, \ 2)
12 Logical operator NOT
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 '~='.
13 GNU Octave Control Package
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 down-
loaded, compiled and installed with the command pkg in-stall 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
. For more information about the
Control Package, view the PDF manual in the packages
doc folder.
Small differences exist - an example is c2d. Here are the
two formats for the bilinear transformation with an analog
model C:
discrete = c2d(C,0.5,'tustin'); % Matlab
discrete = c2d(C,0.5,'bi'); % GNU Octave
14 Some other differences
MATLAB uses the percent sign '%' to begin a com-
ment. Octave uses both the hash symbol # and the
percent sign % interchangeably.
For exponentiation, Octave can use ^ or **; MAT-
LAB requires ^.
For string delimiters, Octave can use ' or "; MAT-
LAB 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 fc-nchk, you can just put the following into a file called
fcnchk.m and put it somewhere Octave can find it:
-
7/23/2019 MATLAB Programming Differences Between Octave and MATLAB
3/4
3
function f=fcnchk(x, n) f = x; end
The main difference is thelack of GUIfor 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 re-leased, which is planned to become official with the
next release.
15 Notes about specific functions
For dbstep, in use dbstep"; for dbstep, use db-
next
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 in-
stance, the N=1 option (repeat reading format un-
til 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 permis-
sive 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 ifthe message is empty. In Octave, it results in an er-
ror.
16 References
http://wiki.octave.org/FAQ#How_is_Octave_
different_from_Matlab.3F
17 See also
Octave Programming Tutorial
https://en.wikibooks.org/wiki/Octave_Programming_Tutorialhttp://wiki.octave.org/FAQ#How_is_Octave_different_from_Matlab.3Fhttp://wiki.octave.org/FAQ#How_is_Octave_different_from_Matlab.3Fhttp://wiki.octave.org/FAQ#GUI -
7/23/2019 MATLAB Programming Differences Between Octave and MATLAB
4/4
4 18 TEXT AND IMAGE SOURCES, CONTRIBUTORS, AND LICENSES
18 Text and image sources, contributors, and licenses
18.1 Text
MATLAB Programming/Differencesbetween Octave and MATLAB Source:https://en.wikibooks.org/wiki/MATLAB_Programming/
Differences_between_Octave_and_MATLAB?oldid=2772831Contributors: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
https://creativecommons.org/licenses/by-sa/3.0/https://en.wikibooks.org/wiki/MATLAB_Programming/Differences_between_Octave_and_MATLAB?oldid=2772831https://en.wikibooks.org/wiki/MATLAB_Programming/Differences_between_Octave_and_MATLAB?oldid=2772831