Matlab Programming Techniques

This workhop uses selected topics from the online self-paced course from the MathWorks in the link below. Participants must have a Mathworks account in order to access the links in this document.

Matlab Academy: Matlab Programming Techniques




Matlab Programming Review

The section is a review of the basic programming constructs presented in the Matlab Fundamentals tutorial.

Logical operations


**[Documentation: if,elseif,else](https://www.mathworks.com/help/matlab/ref/if.html)**

Exercise: Logical Operations

Decision Branching



Exercise: Using if-elseif-else

The switch-case Construction

Exercise: Using switch-case

For Loops

Video: For Loops




**[Exercise: Looping Through a Vector](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlbe#chapter=14&lesson=5§ion=6)**

While Loops

To use a for loop, you need to know in advance how many iterations are required. If you want to execute a block of code repeatedly until a result is achieved, you can use a while-loop.

Slide: The while-Loop Construction



Exercise: Finding eps

Creating and Calling Functions


**[Slide: Creating and Calling Functions](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlbe#chapter=15&lesson=2§ion=1)**



**[Exercise: Create and Call a Function](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlbe#chapter=15&lesson=2§ion=4)**

Slide: Creating Functions Files

Calling Function Files



Exercise: Compare Using a Tolerance



Storing Heterogenous Data

Matlab Data Types


**[Slide: MATLAB Data Types](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=2&lesson=1§ion=1)**

Exercise: Creating Variables of a Specific Data Type

Table Basics


**[Documentation: Tables](https://www.mathworks.com/help/matlab/ref/table.html)**

Exercise: Create a Table from Workspace Variables

Extracting Data from a Table


**[Exercise: Extracting Data from a Table: Dot Notation](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=2&lesson=5§ion=1)**

Exercise: Extracting Data from a Table: Curly Braces

Cell Array Basics


**[Documentation: Cell Arrays](https://www.mathworks.com/help/matlab/ref/cell.html)**

Exercise: Creating and Concatenating Cell Arrays

Exercise: Cell Array Extraction

Structures


**[Documentation: Structures](https://www.mathworks.com/help/matlab/ref/struct.html)**

Exercise: Create a Structure and Access Fields

Exercise: Create a Structure Array

Structuring Heterogenous Data

Structuring Data Considerations


**[Slide: Structuring Data Considerations](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=3&lesson=1§ion=1)**

Exercise: Cell Array

Exercise: Structure Array

Exercise Table

Extracting Multiple Elements from Cell and Structure arrays


**[Slide: Gathering Output](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=3&lesson=2§ion=2)**

Exercise: Multiple Elements from a Cell Array

Exercise: Multiple Elements from a Structure Array

Function Handles


**[Using Function Handles](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=3&lesson=3§ion=1)**

Exercise: Optimization

Applying Scalar Functions to Arrays





**[Video: Applying Scalar Functions to Arrays](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=3&lesson=4§ion=2)**

Exercise: Applying Functions to Groups

Exercise: cellfun Basics

Converting Data Types


**[Slide: Converting Data Types](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=3&lesson=5§ion=1)**

Exercise: Numeric to Cell Array

Exercise: Converting Strings

Managing Data Efficiently


**[Slide: Datatypes and Memory](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=4&lesson=1§ion=1)**

Quiz 1

Quiz 2

Video: Preallocation

Slide: Preallocating Numeric, Cell, and Structure Arrays

Exercise: Preallocation Experiment

Vectorization


Copy-on-write with Function Parameters


**[Slide: Copy-on-write Behavior](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=4&lesson=6§ion=1)**

In-place Optimizations


**[Slide: In-place Optimizations](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=4&lesson=7§ion=1)**

Nested Functions


**[Slide: Nested Functions](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=4&lesson=8§ion=2)**

Exercise: Create a Nested Functions

Creating Flexible functions


**[Video: Creating Flexible Function Interfaces](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=5&lesson=1§ion=1)**

Creating Multiple Interfaces with Wrapper Functions


**[Slide: Separating the Interface from the Algorithm](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=5&lesson=2§ion=1)**

Exercise: Create a Wrapper Function with a Fixed Interface

Setting Default Input Values


**[Slide: Setting Default Input Values](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=5&lesson=5§ion=1)**

Missing Input Arguments


**[Slide: Missing Input Arguments](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=5&lesson=5§ion=2)**

Slide: Empty Input Arguments

Exercise: Skipping the Input

Allowing Any Number of Inputs


**[Slide: Functions with a Variable Number of Inputs](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=5&lesson=6§ion=1)**

Slide: Variable Length Input Argument List

Slide: Passing Argument Lists to Another Function

Allowing a Variable Number of outputs


**[Slide: Defining Different Behaviors Based on Outputs](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=5&lesson=8§ion=1)**

Exercise: Matrix or Vector Output

Changing the Function Interface with Anonymous Functions


**[Slide: Modifying Function Interfaces](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=5&lesson=9§ion=1)**

Slide: Wrapping Functions with Anonymous Functions

Exercise: Write and Use an Anonymous Function

Slide: Common Function Handle Uses

Exercise: Change Function Interface with an Anonymous Function

Creating Robust Applications

Restricting Access Using Private Functions


**[Slide: Making Functions Private](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=6&lesson=2§ion=2)**

Writing Local Functions

Comparison of Functions


**[Exercise: Create Local Functions](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=6&lesson=3§ion=4)**

Validating Function Inputs


**[Quiz](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=6&lesson=5§ion=4)**

Verifying Application Behavior

Why Use a Testing Framework?


**[Video: Why Use a Testing Framework](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=7&lesson=1§ion=1)**

What is a Test


**[Slide: Elements of a Test](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=7&lesson=2§ion=1)**

is Functions


**[Exercise: isequal Versus ==](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=7&lesson=2§ion=3)**

Test Response


**[Exercise: assert](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=7&lesson=2§ion=5)**

Writing and Running a Test Script

Writing a Test Script


**[Exercise: Write a Test Script](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=7&lesson=3§ion=3)**

Running a Test Script


**[Slide: Running a Test Script](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=7&lesson=3§ion=4)**

Exercise: Run a Test Script

Exercise: Fix Broken Tests

Utilizing Development Tools

Developing and Maintaining Code

Folder Reports


**[Slide: Folder Reports](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=8&lesson=2§ion=2)**

Errors and Debugging


**[Video: Different Kinds of Errors](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=8&lesson=3§ion=1)**

Code Analyzer


**[Slide: Suppressing and Fixing Code Analyzer Warnings](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=8&lesson=4§ion=2)**

Exercise: Remove Code Analyzer Warnings

Debugging Runtime Errors


**[Slide: Debugging Run-Time Errors](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=8&lesson=5§ion=3)**

Measuring Performance


**[Slide: Tic and Toc](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=8&lesson=6§ion=2)**

Finding Bottlenecks


**[Video: The MATLAB Profiler](https://matlabacademy.mathworks.com/R2019a/portal.html?course=mlpr#chapter=8&lesson=7§ion=2)**