MATLAB Language

Topics related to MATLAB Language:

Getting started with MATLAB Language

Graphics: 2D Line Plots

Introduction to MEX API

Vectorization

Common mistakes and errors

Object-Oriented Programming

Debugging

Performance and Benchmarking

Using serial ports

Financial Applications

Documenting functions

  • Help text can be located before or after the function line, as long as there is not code between the function line and the start of the help text.
  • Capitalization of the function name only bolds the name, and is not required.
  • If a line is prepended with See also, any names on the line that match the name of a class or function on the search path will automatically link to the documentation of that class/function.
    • Global functions can be referred to here by preceding their name with a \. Otherwise, the names will first try and resolve to member functions.
  • Hyperlinks of the form <a href="matlab:web('url')">Name</a> are allowed.

Programming Utilities

For loops

Iterate over column vector

A common source of bugs is trying to loop over the elements of a column vector. A column vector is treated like a matrix with one column. (There is actually no distinction in Matlab.) The for loop runs once with the loop variable set to the column.

% Prints once: [3, 1]
my_vector = [1; 2; 3];
for i = my_vector
    display(size(i))
end

Altering the iteration variable

Altering the iteration variable changes its value for the current iteration, but has no impact on its value in subsequent iterations.

% Prints 1, 2, 3, 4, 5
for i = 1:5
    display(i)
    i = 5; % Fail at trying to terminate the loop
end

Special case performance of a:b in right-hand side

The basic example treats 1:n as a normal instance of creating a row vector and then iterating over it. For performance reasons, Matlab actually treats any a:b or a:c:b specially by not creating the row vector entirely, but instead creating each element one at a time.

This can be detected by slightly altering the syntax.

% Loops forever
for i = 1:1e50
end
% Crashes immediately
for i = [1:1e50]
end

Fourier Transforms and Inverse Fourier Transforms

Matlab FFT is a very parallelized process capable of handling large amounts of data. It can also use the GPU to huge advantage.

ifft(fft(X)) = X

The above statement is true if rounding errors are omitted.

Undocumented Features

MATLAB User Interfaces

MATLAB Best Practices

This topic displays best practices that the community has learned over time.

Set operations

Image processing

Conditions

Drawing

Useful tricks

Multithreading

Using functions with logical output

Functions

Ordinary Differential Equations (ODE) Solvers

Matrix decompositions

Interpolation with MATLAB

Integration

Graphics: 2D and 3D Transformations

Initializing Matrices or arrays

These functions will create a matrix of doubles, by default.

Reading large files

Usage of `accumarray()` Function

Controlling Subplot coloring in Matlab

The only thing that you need to work out by yourself is the positioning of the colorbar (if you want to display it at all). this will depend on the number of graphs you have, and the bar's orientation.

Position and size is defined using 4 parameters - x_start, y_start, x_width, y_width. The plot is usually scaled to normalized units so that the bottom-left corner corresponds with (0,0) and the top-right to (1,1).