MATLAB Code Standard
Revision 1.0
This code standard is applicable for ALL scripts being submitted for evaluation, which
includes Exams, Homework, and Checks-For-Understanding (CFU’s). However, for Exams
and CFUs, you should apply Section VII only if time permits.
1. VARIABLE CONVENTIONS
1.1. The first character of each word making up the name of a variable must be
capitalized. The remaining characters must be lowercase. If a variable name
contains more than one word, the words may be separated by an underscore.
Velocity
VelocityOfThePlane
Velocity_Of_The_Plane
1.2. Variables should be descriptive, imaginative, and meaningful, but not excessive
in length. The following are bad examples.
DistanceIHaveTraveled
Cnum
1.3. Exceptions may be made to this rule for loop and matrix indices, although the
convention Index#, Row#, or Col# is preferred. One letter variable names must
be uppercase.
I
J
Index1
Col2
2. LINE LENGTH
2.1. Lines must not exceed 80 character in length. If a line exceed the 80 character
limit, the command must be broken between multiple lines using the “…”
operator. It is strongly advised that lines be broken at 75 characters to avoid
any possible chance of exceed the 80 character limit.
3. SPACING
3.1. Tabs should not be used, as a tab does not have a standard length across
operating systems or even programs on the same operating system.
Instead, pad lines with spaces. One space must be placed between
arithmetic or logical operators and their operands.
NewTemperature = InitialTemperature * 50;
My_Grade = 70 + Exam_Score;
if ( Exam_Score >= 90 )
3.2. One space must be placed after semicolons and commas.
SampleMatrix = [ 1 2 3 4; 4 3 2 1 ];
subplot(2, 1, 2);
3.3. Parameter lists may be preceded by an optional space.
title(‘A Graph’);
title(‘A Second Graph’);
Last Document Update: 01.11.2019 1
4. INDENTATION
4.1. Control structures must be indented three to five spaces from the above block
of code. Only one distance of indentation may be used in a given program.
for IndexY = 1 : Columns
Totals(7, IndexY) = sum(Totals(2 : 6), IndexY));
end
5. COMMENTS
5.1. Comment lines should be frequently used to explain the function of given
blocks of code. They should explain the intention of the code, not the obvious.
The comment text should be separated from the percent sign by one space.
Good example.
% Initial temperatures in degrees Celsius.
Temperatures = [ 10 34 23 ];
Bad example.
% Variable to store temperature values.
Temperatures = [ 10 34 23 ];
5.2. Comments should be placed above the block of code that they explain, and
they should be indented in line with the code.
% For each score in the class…
for I = 1 : size(Scores, 1)
% Record the letter grade and print the score.
fprintf(‘%I’, Scores(I, 2 : end));
end
5.3. The comment may appear on the same line as the commented code for
variable declaration or initialization and to note the end of control structures.
Totals = zeros(1, 6); % Total # of grades
for I = 1 : Students
Totals(7, I) = sum(Totals(2 : 6), I));
end % for I = 1 : Students
5.4. There should always be a blank line above a line of comment.
5.5. For comments longer than one or two lines that regard program development,
operation, error testing, output interpretation, etc., there should be a blank
line above and below the comment block.
5.6. Each function or M-file should include enough comments at the top of the
function to allow any user to understand the purpose and syntax of the
function when using the “help” command.
Last Document Update: 01.11.2019 2
6. PROGRAM HEADING INFORMATION
6.1. For an individual assignment:
% Activity XX: An introduction to pretty MATLAB code
% File: filename.m
% Date: 1 January 2019
% By: Full Name
% UCusername
% Section: Number
% Team: Team Number
%
% ELECTRONIC SIGNATURE
% Full Name
%
% The electronic signature above indicates the script
% submitted for evaluation is my individual work, and I
% have a general understanding of all aspects of its
% development and execution.
%
% A BRIEF DESCRIPTION OF WHAT THE SCRIPT OR FUNCTION DOES
%
6.2. For a team assignment:
% Activity XX : An introduction to pretty MATLAB code
% File: filename.m
% Date: 1 January 2019
% By: Full Name team member 1
% UCusername
% Full Name team member 2
% UCusername
% Full Name team member 3
% UCusername
% Full Name team member 4
% UCusername
% Section: Number
% Team: Team Number
%
% ELECTRONIC SIGNATURE
% Full Name team member 1
% Full Name team member 2
% Full Name team member 3
% Full Name team member 4
%
% The electronic signatures above indicate the script
% submitted for evaluation is the combined effort of all
% team members and that each member of the team was an
% equal participant in its creation. In addition, each
% member of the team has a general understanding of all
% aspect of the script development and execution.
%
% A BRIEF DESCRIPTION OF WHAT THE SCRIPT OR FUNCTION DOES
%
Last Document Update: 01.11.2019 3
7. CLEAN UP
7.1. You should clear all variables at the beginning of your script.
7.2. You should not clear any variables at the end of your script.
8. LOOPHOLE FOR ABSENT-MINDED GTA’S, PEER TEACHERS, AND PROFESSORS
8.1. We understand that swallowing a full code standard at one time may be a bit
much. However, being able to write code that is neatly organized an uniform
will go a long way to your development as a programmer. A consistent (and
logical) style greatly improves the readability of your code. The makes it easier
for us to quickly help you, grade your assignments, and award partial credit.
You will also find that a consistent style will help you to find errors in your
code.
8.2. We recognize that we have not addressed every possible situation. Our intent
is to provide you a framework to aid you in developing good programming
habits. If you find something that we did not cover in this standard, use your
best judgment, or ask a GTA or Peer Teacher.
8.3. You may also wish to read the code standard for other courses at the University
of Cincinnati. Although not required for this course, they will give you an idea
of what other programming courses require of you.
Last Document Update: 01.11.2019 4