## Purpose

MATLAB combines a desktop environment tuned for iterative analysis and design processes with a programming language that expresses matrix and array mathematics directly.

MATLAB is popular among academic researchers. Since many of the startups we work with are spun out of academic labs, we occasionally need to work with research-grade MATLAB code. Sometimes, we will be migrating this MATLAB code to Python or C++. Other times, we will keep some or all parts of a project in MATLAB. In fact, we’ve had few MATLAB-only Software as a Medical Device (SaMDs) 510(k) submissions. Usually we take this route when migrating away from MATLAB would be difficult or expensive and the startup has limited funding.

Since most professional software engineers don’t know MATLAB, we’ve put together this lesson to help people pick up the language quickly. Since MATLAB and Python are, to some extent, competing technologies, this article does refer to Python occasionally. If you don’t know Python, that should be okay. This lesson isn’t meant to turn you into a MATLAB expert. It is meant to cover the basics that all new MATLAB developers should be familiar with.

## Learning Material

### Cost and Licenses

To develop in MATLAB you will need a commercial license. These licenses cost a few thousand dollars, and many projects will require licenses to additional MATLAB toolboxes, each of which costs a few hundred to a few thousand dollars. We tend to use several of the same Toolboxes:

- Image Processing Toolbox
- Deep Learning Toolbox
- Signal Processing Toolbox
- Parallel Computing Toolbox
- Curve Fitting Toolbox
- Optimization Toolbox
- Statistics and Machine Learning Toolbox
- Database Toolbox
- MATLAB Report Generator
- MATLAB Compiler

When working with MATLAB, you may run into situations where you need to buy a toolbox just to use a simple function that wouldn’t take long to code up yourself. We’ve started collecting a library of these simple functions, which we encourage you to use and add to.

### Strengths and Weaknesses

Why do researchers like MATLAB? Unlike software engineers working on production systems, researchers tend to care about iterating quickly on their own code than they do about writing robust and readable code that will be used by a team. They also tend not to be professional software developers. Given these different user needs, its understandable that MATLAB made tradeoffs to be well-suited for one group but less well-suited for the other. Here’s a small example of this sort of tradeoff: MATLAB has 1000’s of built-in functions that are available without needing to import them. This is convenient for researchers with small programs. It is a source of namespace collisions on larger projects.

As engineers and consultants, we need to make tradeoffs to maximize the value we deliver to our clients. Keep this in mind when you’re working on MATLAB and in particular when you’re considering migrating away from MATLAB.

**MATLAB’s Strengths**:

- Many researchers and scientists are familiar with the software (this matters, e.g., if researchers on the team want to continue contributing to parts of the code).
- Has a lot of high-quality builtin mathematical functions. Some of these
aren’t easily available outside MATLAB. For example, MATLAB’s
`griddata`

interpolation function supports natural neighbor interpolation. A few years back, we looked for a similar library in Python, but ended up having to write our own (which was time consuming and expensive.) - MATLAB’s plotting utilities are excellent—in most ways they are easier to use and more performant than anything available in Python
- Generally high-quality documentation

**MATLAB’s Weaknesses**:

- The language and the libraries are not as general purpose as Python
- There are fewer developers that know how to use it
- It gets expensive as you install more toolboxes
- Python’s deep learning ecosystem is better; for example, TensorFlow’s automatic differentiation seems to work better than MATLAB’s
- Not as easy to install in cloud environments
- GUI’s built with MATLAB tend to be slow and highly constrained; its not uncommon to make it 90% of the way through a project and then to realize that the last 10% is difficult or impossible to accomplish.

### Basics

The MATLAB documentation available online is pretty good. To begin, we recommend installing MATLAB and then reading through these pages:

- Desktop Basics
- Matrices and Arrays
- Array Indexing
- Text and Characters
- Fundamental MATLAB Classes
- Glance at Data Types, and in particular Tables, Structures, Cell Arrays, and Function Handles
- Scripts vs. Functions
- Debug a MATLAB Program
- Glance through the components and widgets available for uifigure-based Apps
- Glance through the Testing Framework documentation

### Gotchas

- Indexing begins with 1 and not 0.
- Semicolons aren’t required; all they do is suppress MATLAB from automatically printing the output of a line. Thus semi-colons should almost always be used.
- If you want to call a function from several other functions or scripts then you’ll need to create a single file for each function. This gets very tedious. Note that if you only use functions within the current file, you can define them at the end of the file.
- Strings are newer than character arrays; most functions accept both, but occasionally there are situations where you must use one or the other.
- The default data type in MATLAB is a matrix. You can have an empty matrix (size 0x0), a column vector (e.g., size 10x1), a row vector (e.g., size 1x10), a matrix (e.g., 5x10), or higher dimensional matrices (e.g., 512x512x10x3). Note there is no such thing as a simple vector. Row vectors and column vectors act differently. Thus, MATLAB has a unique type of bug—the “I need a row vector but got a column vector” bug.
- There are uifigure-based Apps and figure-based Apps. The uifigure ones are newer and what we recommend using.

### Best Practices

Since many MATLAB projects begin as research code, it is not uncommon to inherit codebases with

- very long functions
- non-descriptive variable names
- lots of dead and commented out code
- inconsistent code-formatting
- no tests.

When working on suction projects, we encourage you to iteratively clean up all of this.

### Other Resources

- Undocumented MATLAB is a blog by a MATLAB expert who stretches MATLAB to the limits.
- NumPy for MATLAB users; these tables can also be used for NumPy users who are learning MATLAB
- Enthought’s “Migrating MATLAB to Python” guide

## Exercises

To learn as much as possible from these exercises, **write** your responses **before revealing** the provided answers. If any exercises seem irrelevant, you can skip them and instead write a justification as to why they are unimportant. These justifications will help us improve the lesson for future employees.

### Exercise 1

Look up the difference between the `*`

operator and the `.*`

operator in
the MATLAB documentation. Without running it, what does this program output?

```
5*[1 2 3]
5.*[1 2 3]
[1 2]*[4; 5]
[1 2].*[4; 5]
```

### Exercise 2

Clone our matlab-utils git repository. Open the code in MATLAB and run the tests. Verify that they all pass. Set a debugger breakpoint in one of the tests, run them, and step through the code using the debugger.

### Exercise 3

Select a small TODO in the matlab-utils code and implement it, writing tests as you go.