# MATLAB Programming/Differences between Octave and MATLAB

[MATLAB Programming\|/MATLAB Programming]m]
Chapter 1: MATLAB ._.
    Fundamentals of MATLAB
    MATLAB Workspace
    MATLAB Variables
    *.mat files
Chapter 2: MATLAB Concepts
    MATLAB operator
    Data File I/O
Chapter 3: Variable Manipulation
    Numbers and Booleans
    Strings
    Portable Functions
    Complex Numbers
Chapter 4: Vector and matrices
    Vector and Matrices
    Special Matrices
    Operation on Vectors
    Operation on Matrices
    Sparse Matrices
Chapter 5: Array
    Arrays
    Introduction to array operations
    Vectors and Basic Vector Operations
    Mathematics with Vectors and Matrices
    Struct Arrays
    Cell Arrays
Chapter 6: Graphical Plotting
    Basic Graphics Commands
    Plot
    Polar Plot
    Semilogx or Semilogy
    Loglog
    Bode Plot
    Nichols Plot
    Nyquist Plot
Chapter 7: M File Programming
    Scripts
    Comments
    The Input Function
    Control Flow
    Loops and Branches
    Error Messages
    Debugging M Files
Chapter 8: Advanced Topics
    Numerical Manipulation
    Advanced File I/O
    Object Oriented Programming
    Applications and Examples
    Toolboxes and Extensions
Chapter 9: Bonus chapters
    MATLAB Benefits and Caveats
    Alternatives to MATLAB
    [MATLAB_Programming/GNU_Octave|What is Octave= (8) hsrmonic functions]
    Octave/MATLAB differences
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, 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.

## C-Style Autoincrement and Assignment operators

Octave supports C-style autoincrement and assignment operators:
MatLab does not.

## Product of booleans

MATLAB (R2011b) and Octave (3.6.4) respond differently when computing the product of boolean values:
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.

## Character Strings and Arrays

MATLAB differentiates between character strings and character arrays, while Octave does not. In Octave:
In MATLAB:
Solution: Use strcat for character string concatenation.

## ['abc ';'abc']

['abc ';'abc'] is now allowed in both Octave and MATLAB; (MATLAB previously would return: ?? Error using ==> vertcat)
In Octave and MATLAB the result will be a 2 by 3 matrix.

## 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:
Now "S STRING" will evaluate the string in the shell.

## Attempting to load empty files

MATLAB lets you load empty files, OCTAVE does not.

## fprintf and printf

Octave supports both printf and fprintf as a command for printing to the screen. MATLAB requires fprintf:
fprintf covers writing both to the screen and to a file by omitting the optional file-handle argument:

## Whitespace

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

## Line continuation

MATLAB always requires ... for line continuation.
while Octave also supports
For both programs, a line break without '...' within an array shifts to the next row

## Assignment

Octave supports
MATLAB requires
Octave allows
while MATLAB requires
This example also shows how to detect the interpreter at runtime.

## 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 '~='.

## 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 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.
Small differences exist - an example is c2d. Here are the two formats for the bilinear transformation with an analog model C:

## Some other differences

* 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 ^.
* 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) needs a function named fcnchk, create a file named fcnchk.m with the contents shown below and put it where Octave can find it:
* The main difference used to be the lack of GUI for Octave. With version 4.0 Octave has a GUI as its default interface.

## Notes about specific functions

* For "dbstep, in" use "dbstep"; for "dbstep", use "dbnext"
* For "eig(A,B)" use "qz(A,B)"
* The fputs function is not available in MATLAB. Use fprintf instead.
* The strftime function is not available in MATLAB. Use datestr instead.
* The time function is not available in MATLAB. Use now instead.
* As of 4.2.1, Octave does not print outputs to the console until it has completed all waiting commands, unlike MATLAB. This behavior is controlled by the boolean variable page_output_immediately (default: 0), and it is not ameliorated in --traditional.
* The functions 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.
* The 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.
* 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
* Octave FAQ: What features are unique to Octave?

## See also

* Octave Programming Tutorial