Show Menu
Cheatography

MATLAB Repo Cheat Sheet (DRAFT) by [deleted]

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

Naming conven­tions

Variable names in camel case
moveTo­Pos­ition
Functions & methods in camel case
sendFi­les­Ove­rNe­twork
Acronyms follow camel case
htmlPage, isJpeg
Use meaningful names
wage = hourlyRate * nHours
n prefix for amounts
nFiles
No suffix for specific numbers
measur­ementNo
Cnt suffix for iterator variables
sampleCnt = 1:nSamples
Constant uppercase + underscore
SPEED_­OF_­LIGHT
Classes & stuctures capita­lized
RadarS­ensor
Non-de­fault units in variable name
BOTTLE­_CA­PAC­ITY_CL = 50

Layout

Split lines if needed
longMe­tho­dNa­me(... 
long, list, of, params)
Align for better readab­ility
value = (10 * nDimes) + ... 
        (5  * nNickels) + ...
        (1  * nPennies);
Commas are followed by spaces
add(a, b, c)
Brackets are NOT surrounded by spaces
mean(p­rof­itP­erM­onth)
Separate logical groups of code by a blank line
Separate functional groups of code by using
%%

Best practices

Write code as functions when possible
The main role of scripts is in develo­pment because they provide direct visibility of variables. Functions modularize comput­ation by using internal variables, and tend to be cleaner and more flexible. It is also much easier to manage changes if code appears in only one file.
Don't (always) use
clear all
Whenever Matlab meets new code it pre-co­mpiles it, making re-runs faster. Make sure you don't use
clear all
when you meant to use
clear
- the former not only clears variables but also the pre-co­mpiled code.
Helper functions
A single .m file can contain multiple functions. Although only the first function is exposed, this allows for better struct­uring and reuse of the code.
Subfun­ctions
It's possible to nest functions, allowing for better struct­uring while strongly associ­ating the subfun­ction with it parent.
 

Comments

Write comments only when needed
For comments after code, prefix '␣␣%␣'
SAMPLE­_FREQ = 3e9 ­ ­% Hz
Align for better readab­ility
var = 1;       %­ ­Comment 
otherVar = 2;  %­ ­Other comment

Docume­ntation

Class header gives a descri­ption of the class and a summary of all its functions
Function headers give inform­ation about the inputs & outputs
Include a demo file for non-tr­ivial classes and functions

Speed-ups

Preall­ocation
Allocate empty matrices to store increm­ental results generated by loops. If the number of results is unknown, you can allocate the for maximum amount and prune afterward (if possible).
Vector­ization
Placing a period (.) before the operators *, /, and ^, transforms them into array operators.
Some functions also accept vector­s/m­atrices as input and perform their comput­ations on elemen­t/r­ow-­wise.

Example
 t = 0:.01:10;
y = sin(t);
Indexing
Use vector or boolean indexing instead of loops where possible

Example
 someNu­mbe­rs(­som­eNu­mbers < 0) = 0; 
Profiler
To determine which parts of your code are the most time consuming use the
tic toc
,
timeit
, or
profile
commands.

Workflow

Create a new branch for your feature
git checkout -b featur­ename
Use git as you normally would
git add file1.m file2.m

git commit -m "message"

git pull origin featur­ename

git push origin featur­ename
Regularly merge the master branch into the feature branch
(git checkout featur­ename)

git fetch origin

git merge origin­/master
One the feature is completed, file a merge request on GitLab.
If your request if rejected.

Sources

Guidelines for writing clean and fast code in MATLAB - Nico Schlömer
MATLAB Style Guidelines 2.0 - Richard Johnson
The Elements of MATLAB Style - Richard Johnson