Show Menu
Cheatography

MATLAB Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

fprintf

A1 = [9.9, 9900];
A2 = [8.8,  7.7 ; ...
      8800, 7700];
formatSpec = 'X is %4.2f meters or %8.3f mm\n';
fprintf(formatSpec,A1,A2)

Output:
X is 9.90 meters or 9900.000 mm
X is 8.80 meters or 8800.000 mm
X is 7.70 meters or 7700.000 mm
%4.2f in the formatSpec input specifies that the first value in each line of output is a floati­ng-­point number with a field width of four digits, including two digits after the decimal point. %8.3f in the formatSpec input specifies that the second value in each line of output is a floati­ng-­point number with a field width of eight digits, including three digits after the decimal point. \n is a control character that starts a new line.

Line Properties

Color:
red = r
green = g
blue = b
cyan = c
magenta = m
yellow = y
black = k
white = w
none

LineStyle:
solid line = -
dashed line = --
dotted line = :
dash dotted line = -.
no line = none

Marker:
circle = o
plus sign = +
asterisk = *
point = .
cross = x
square = square or s
diamond = diamond or d
none = none

Marker­Edg­eColor & Marker­Fac­eColor:
same as color

Marker­Indices
 

max

A = [23 42 37 18 52];
M = max(A)

Output: 
M = 52

A = [2 8 4; 7 3 9]
M = max(A);

Output:
M = 1×3

     7     8     9

mean

A = [0 1 1; 2 3 2; 1 3 2; 4 2 2]

     0     1     1
     2     3     2
     1     3     2
     4     2     2
M = mean(A);

Output:
  1.7500    2.2500    1.7500

A = [0 1 1; 2 3 2; 3 0 1; 1 2 3]

     0     1     1
     2     3     2
     3     0     1
     1     2     3

M = mean(A, 2);

Output:
    0.6667
    2.3333
    1.3333
    2.0000

plot examples

plot examples

 

sum

A = 1:10;
S = sum(A)

Output:
S = 55

fprintf

a = [1.02 3.04 5.06];
fprintf('%d\n',round(a));

Output:

1
3
5
%d in the formatSpec input prints each value in the vector, round(a), as a signed integer.

building arrays­/loops

max loop