style guide for matlab functions

Upload: muhammad-enam-ul-haq

Post on 03-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Style Guide for Matlab Functions

    1/3

    Style Guide for Matlab Functions

    Every function begins with a header, delineated by a string of lines with % asthe 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 withconsistent spacing. The TA will have an easier time grading such a program,and you'll make fewer mistakes. In particular, note

    1. how tabs and blank lines in the header set off different parts of theheader and make it more readable;

    2. aligning equal signs in consecutive lines makes reading equations

    easier;3. cos(theta) and sin(theta) are computed only once, and stored intemporary 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;

  • 7/29/2019 Style Guide for Matlab Functions

    2/3

    stheta ctheta 0;0 0 1];

    Below is another example.1. the indentation in the ifconstruction delineates clearly that lines like k =

    zeros(3,1);are part of the ifstatement.2. For arithmetic operations like - + * /, in general please put spaces

    around them for better readability; an exception is inside matrixelements to avoid misinterpretation as separate elements.

    3. Put in comment lines to explain particular choices or computations; forexample, there is an explanation that the positive square root waschosen forsin(theta).

    4. Note also the efficiency in computing the intermediate result dR, whichavoids taking the differences of the included elements like R(3,2)-R(2,3) twice.

    5. Note the comparison ofabs(theta) to the machine precision eps, to testfor equality to zero. With floating point computations, one cannot testexactly 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)>epsk = dR / (2 * stheta);

    elsek = zeros(3,1);

    end

  • 7/29/2019 Style Guide for Matlab Functions

    3/3