MATLAB (WIP)

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:

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:

MATLAB’s Weaknesses:

Basics

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

Gotchas

Best Practices

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

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

Other Resources

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 students.

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]
Answer
ans =
     5    10    15

ans =
     5    10    15

ans =
    14

ans =
     4     8
     5    10

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.

Continuous Lesson Improvement

Please help us make these lessons as relevant and up-to-date for future engineers as possible!

You can help in several ways:

  • Shorten or clarify the writing. We're all busy and less is more.
  • Ask if the purpose of the lesson is unclear. We want all of the lessons to seem useful.
  • Remove exercises or learning material that aren't useful.
  • Add more exercises, exercise answers, or learning material as appropriate.

You can quickly open the lesson page in the GitHub editor. Create a new branch and pull request and assign it to David.