Find us on GitHub

Teaching basic lab skills
for research computing

MATLAB: Introduction

MATLAB/Introduction at YouTube

Hello, and welcome to the Software Carpentry series on MATLAB programming. This is the first episode in the series, and we will briefly tour the world of MATLAB.

We start by making a few observations on what scientific software should look like. Probably the most important aspect of good software is that it is written for humans rather than machines.

Humans are the ones who have to understand, critique, and debug software, and your task as a programmer is to make it easier on your fellow programmers.

Software written for humans is, first of all, written in a readable format. The structure and purpose of code should be obvious.

Software written for humans emphasizes what is important. Usually, we write code to solve particular problems in particular domains. Good code should emphasize those domains rather than the low level algorithms that are utilized.

Good code should always be testable, which means that its purpose must be clear enough that tests can be written.

Finally, good code should be efficient. We will see how MATLAB provides efficiency without sacrificing readability and testability.

Contrast good code with bad code. Bad code is hard for humans to read. It emphasizes small efficiency gains over human readability and testability. Often, it re-implements functions that have already been written and tested by someone else. Also, it can be hard to find the domain specific algorithm among all of the low level, secondary routines.

MATLAB is a tool to help scientists write good, efficient programs, and write them quickly. It provides a wide array of efficient mathematical routines that have been tested by someone else. Also, it provides a high level programming language that helps programmers express their work as mathematical ideas rather than programming details.

On most systems, MATLAB opens a graphical environment like this one.

It includes a list of all variables that have been created.

The center of the window is a command line where we can create and manipulate variables.

Most programs are written in the editor, which provides a way to create functions that can be called from other functions or from the MATLAB command line. We will cover programs in two lectures on flow control.

The MATLAB programming language is built around the multi-dimensional floating point array. We will cover floating point arrays in the lectures on programming basics, array indexing, and linear algebra. Typical flow control structures like for loops, while loops, and if statements are available and are introduced in the lectures on flow control.

Throughout this lecture series, we will return to the idea of data parallel programming. In data parallel programs, the programmer is encouraged to think at a high level of abstraction. Implementing algorithms, which typically takes a lot of loops and indices, is handled by the programming language. This means that code is easier to write and debug, and typically, data parallel programs are much faster.

This simple example demonstrates the difference between a typical program that emphasizes loops and a data parallel implementation of the same thing. The data parallel approach places more emphasis on the objective of the operation than on the implementation of the objective. We will see numerous examples of how data parallel thinking can simplify and speed up your code.

So why learn MATLAB? Over the last 20 years, MATLAB has become one of the most common languages in the scientific programming community. Its wide range of built in functions allows rapid prototyping and it scales well to large, production platforms. Perhaps its biggest strength is the support community, which has provided algorithms based on recent publications across many fields.

In our next episode, we will start to examine the basic data type in MATLAB, which is a multi-dimensional array of double precision numbers.