Week 1 - MATLAB Fundamentals

Lectures, code examples, and practice questions for teaching MATLAB for applications in undergraduate chemical engineering classes.

Week 1 - MATLAB Fundamentals

Reminder - the best place to learn MATLAB (or anything, really) is the internet! StackOverflow and MathWorks’ own MATLAB Exchange are filled to the brim with people asking and answering questions about MATLAB. MATLAB’s own documentation is also extensive and extremely helpful. It includes descriptions of how to call functions as well as usage examples.

Download this page as a PDF here (this document is generated automatically - print this page to PDF from your browser for the best result).

Return to Lectures

What is MATLAB?

Started as a simple program intended to give students an easy way to manipulate matrix-based data, hence Matrix Laboratory. Has grown over time to include support for signal processing, machine learning, solving differential equations, and more.

Which version of MATLAB do I buy?

To satisfy course requirements at the University of Delaware, and just generally to make MATLAB more useful you will need a couple of Toolboxes. These are the MATLAB equivalent of Python packages like numpy or SciPy - they add functionality to the base program. For our program, we recommend the Symbolic Math Toolbox and Curve Fitting Toolbox, both of which can be bought separately or come bundled in the Student Suite. UD has remote computing resources available, but they came with all the typical limitations of remote computing and for the highly discounted price of the Student License for MATLAB I think it is worth saving yourself the headache.

Layout of the MATLAB User Interface

The main window of MATLAB has three major components, the Current Folder, Command Window, and Workspace: Image of Default MATLAB Layout

(taken from MathWorks)

The current folder is the same as your File Explorer. It shows you the folder that MATLAB currently has “open” and would be able to access files from in a script.

The Command Window is synonymous to the Python interpreter. Commands issued here are interpreted, and their output written to the command window.

The Workspace is the same as a function workspace, except that it persists between running scripts and even when no scripts are running. This makes prototyping code very simple, as you can do something hack-y to get the variable you want, save the resulting variable, and then reuse for later use and (hopefully) better code.

Here’s an example:

How MATLAB stores your data

MATLAB stores values as double-precision floating point numbers arranged in a Matrix. Matrices are indexed in order of their dimensions, such as this:

my_value = my_matrix(row, column, layer, dimension_4, ...)

Thinking in matrices may seem uncomfortable at first, but it actually offers some unique advantages. Imagine storing the results of a gas chromatography run in a matrix. In Python, you might have separate lists for time and peak height, whereas in MATLAB you can natively store both values in two parallel columns of matrix. Storing values in a matrix also decreases processing times. For reasons which are beyond the scope of this course and my comfort level with systems programming (see here) storing data is matrices is highly memory efficient.

MATLAB Syntax

Most (if not all) of you have already taken Python, so I’ll just put it this way: MATLAB is similar to Python but with a few differences:

Scipts and Live Scripts

MATLAB allows you to issue a series of commands (in much the same way as a Python script) via a .m script file. This format is best for writing general purpose functions that you plan to call from other scripts. When calling scripts from other scripts, you use the filename which contains the desired function - MATLAB therefore enforces naming your .m files after the first function in the file. You can have other functions in the file (helper functions), but you will not be able to call them directly.

MATLAB also has another scripting filetype, the MATLAB Live Script or .mlx file. This is somewhat similar to a Jupyter notebook, where output is kept inline with input and can be interacted with, such as manipulating plots. Live scripts are also extremely helpful for debugging, in particular when paired with MATLAB’s excellent built-in debugger.

Plotting

MATLAB uses a plotting library similar to matplotlib but with a handful of helpful features. For example, any figure which you open comes with an attached Property Inspector, from which you can edit every aspect of the plot.

Plotting commands in MATLAB are generally highly intuitive. Here are some example to get you started:

% use a percent sign to leave a comment
% commas in lists are optional
my_data = [1 2 3]; % adding a semicolon to the end of a line will prevent it from generating output
% using plot with only one argument will assume the x-axis data is 1,2,3... etc.
plot(my_data)
% plot with two arguments allows you to specify the x and y data (in that order)
x_data = [0.1 0.2 0.3]
y_data = [37, 64, 8]
plot(x_data, y_data)

See the MATLAB documentation for plot here. Check out an example MATLAB code file that demonstrates the key syntax shown above.