Style Guide for Matlab Functions
Every function begins with a header, delineated by a string of lines
with % as the first character to indicate comment lines.
% FunctionName Followed by a concise function description
% (Blank line)
% FunctionCall followed by
% several lines explaining what
% the function computes in detail.
% (This description plus the subsequent variable definitions
% are all indented.)
% (Blank line)
% Input argument 1 = description of what it is
% Input argument 2 = description of what it is
% etc.
% Output argument 1 = description of what it is
% etc.
% (Blank line)
% Author Name
% Class
% Date
An example is given below. Please look at how tidy the program looks
with consistent spacing. The TA will have an easier time grading
such a program, and you'll make fewer mistakes. In particular, note
- how tabs and blank lines in the header set off different parts of
the header and make it more readable;
- aligning equal signs in consecutive lines makes reading equations easier;
- cos(theta) and sin(theta) are computed only once,
and stored in temporary variables, for efficiency.
%rotz Elemetary rotation matrix about z axis.
%
% R = rotz(theta) returns a rotation matrix representing a
% rotation of theta about the z axis.
%
% theta = angle of rotation about z axis.
% R = 3x3 elementary rotation matrix.
%
% John M. Hollerbach
% CS 5310/6310 or ME 5220/6220 Introduction to Robotics
% September 8, 2003
function R = rotz(theta)
ctheta = cos(theta);
stheta = sin(theta);
R = [ctheta -stheta 0;
stheta ctheta 0;
0 0 1];
Below is another example.
- the indentation in the if construction delineates clearly that
lines like k = zeros(3,1); are part of the if statement.
- For arithmetic operations like - + * /, in general please
put spaces around them for better readability; an exception is
inside matrix elements to avoid misinterpretation as separate
elements.
- Put in comment lines to explain particular choices or
computations; for example, there is an explanation that the
positive square root was chosen for sin(theta).
- Note also the efficiency in computing the intermediate result
dR, which avoids taking the differences of the included
elements like R(3,2)-R(2,3) twice.
- Note the comparison of abs(theta) to the machine precision
eps, to test for equality to zero. With floating point
computations, one cannot test exactly for zero, but only down to
the machine precision.
% rot2axis Extract angle-axis from a rotation matrix.
%
% [theta,k] = rot2axis(R) extracts the angle theta and axis k
% from a given matrix R.
%
% R = input 3x3 rotation matrix
% k = axis of rotation
% theta = angle of rotation
%
% John M. Hollerbach
% CS 5310/6310 or ME 5220/6220 Introduction to Robotics
% September 8, 2003
function [theta,k] = rot2axis(R)
dR = [R(3,2)-R(2,3);
R(1,3)-R(3,1);
R(2,1)-R(1,2)];
ctheta = (trace(R) - 1.0) / 2.0;
% Choose positive root for sin(theta).
stheta = 0.5 * sqrt(dR(1)^2 + dR(2)^2 + dR(3)^2);
theta = atan2(stheta, ctheta);
if abs(stheta)>eps
k = dR / (2 * stheta);
else
k = zeros(3,1);
end
|