Matlab syntax tricks

By Raphaël - December 18th, 2016

Matlab

0 Comment

Arrays

A big asset of the Matlab language is that the syntax is simple to learn. But besides a seamingly poor set of syntax features (e.g. no conditional expression, no modulo operator, no increment/decrement operator, no user-defined literal, etc.), there are several syntax tricks that many programmers ignore.

It is assumed that the reader is already familiar with basic Matlab syntax. Almost everything that is presented here is documented; but the documentation is so vast that, even after many years of Matlab coding, one still finds now and then some little gems in the doc. This post gathers some of those not-totally-mainstream syntax tricks, which may take time to find on one's own and may reveal useful to anyone willing to improve his/her coding style and efficiency.

As an appetizer, let's see what we can do with the first element of syntax everyone has to learn about: comments.

Comments in Matlab are preceeded by a %. To make multi-line comments, one can use the percent brace syntax %{ ... %}:

% Lorem ipsum dolor sit amet, consectetur adipiscing elit,
% sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
% Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
% nisi ut aliquip ex ea commodo consequat.

% --- is equivalent to ---

%{
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat.
%}

Want to see more? The following pages are organized as thematic sections.

Arrays