Master MATLAB: Essential Tips and Tricks That Will Boost Your Coding Efficiency Overnight

Unleash the full potential of MATLAB with these little-known, expert-level tips

Schoepfloeffel
16 min readJan 10, 2024

“MATLAB is an F-Tier programming language”

DALL-E, version 3, OpenAI, 05Jan. 2024, labs.openai.com/

A sentiment echoed within most parts of the programming community, primarily due to its ambiguous syntax, complexities in global namespace management, lack of in-depth documentation, and overly intuitive functions that attempt to anticipate user behaviour. This perspective is particularly prominent when comparing MATLAB to other more general-purpose programming languages. After three years of MATLAB coding and comparing it to my experience with more popular languages like C or Python, I share this sentiment. Despite its quirks, MATLAB is still rather popular and holds rank 14 in the TIOBE index (Nov 2023). I argue that MATLAB is specifically designed for engineers, scientists, quants, and data analysts and less for software engineers. MATLAB is more about interactivity, visuals, and ease of use for the engineering and scientific community.

What is this article about? Whether you’re delving into groundbreaking research or innovative industry applications, this article will provide you with valuable insights and advanced techniques in MATLAB programming. I will go into detail about the most common problems I experienced during my MATLAB journey and how to write more readable and maintainable code. For hands-on learners, I’ve included illustrative code examples in a live script on my GitHub.

Who should read this article? My primary intended audience consists of engineers without an extensive software background who aim to leverage the MATLAB language in their daily work.

Please note, that this article focuses exclusively on MATLAB’s application and coding skills in engineering contexts and does not cover broader programming, software design, or architectural principles. We’ll focus on functional programming within MATLAB, touching upon object-oriented concepts only lightly. If you are a seasoned programmer, you will already be familiar with most of the concepts outlined here.

What are the prerequisites? A good deal of familiarity with MATLAB and knowledge about basic data types.

What will you learn and how will it improve your abilities? This article aims to help you build a solid foundation in writing functions/code and covers the following points:

  • Writing more readable and maintainable code
  • Understanding data types and assessing the use cases
  • Investigating the inner workings of the language to solve daily problems
  • Producing expert-level documentation

Get ready to transform your approach to engineering projects with these advanced MATLAB techniques!

TLDR: Time can be more valuable than money. Scroll to the bottom of the article to get the TLDR!

1. Understand Scripts, Functions and Object-Oriented Programming and when to use it

Scripts are a series of MATLAB commands stored in a file. As practical problem-solvers, engineers often favour scripts for their straightforward, pragmatic approach. However, while scripts are great for ”quick and dirty” solutions, relying solely on them, especially in complex projects, can be precarious. The risk of losing track of variables or facing conflicts is high. The code is bound to break at a certain point.

Functions contain one or more sequential commands. They can accept inputs and return outputs. I see most engineers begin writing functions after their initial “novice phase”. Functions can be more versatile in the parametrization and the isolated namespace enhances modularity and reusability. It’s common to see engineers’ projects evolve into a collection of functions, orchestrated within a “master script”. However, navigating through someone else’s functions can be challenging. Questions often arise about the appropriate data types for arguments, the sequence of function calls, and potential changes in function outputs. To stress this point, you can take a look at the most prominent project on the MATHWORKS file exchange. Because functions are so prominent, the article will focus more on examples of how to write proper functions for your peers.

While Object-Oriented Programming (OOP) is less frequently used in MATLAB compared to functional programming, it becomes immensely valuable when dealing with complex, large-scale applications. OOP allows for sophisticated data abstraction and component-based programming. Think of an object as a custom data structure equipped with its own attributes and methods. This approach can significantly enhance your programming capabilities, especially in structuring large-scale projects. For those interested in delving deeper, I recommend exploring MATLAB’s official resources on OOP, which provide a solid foundation for understanding this powerful programming paradigm and its application in MATLAB.

Summary: Scripts are ideal for simple tasks and personal use. Functions are more versatile, although they require you to follow certain principles for maximum efficacy. These days OOP is not very prominent within the MATLAB community, but we can make it more popular :). If you are working on a complex project, I encourage you to learn some OOP approaches for MATLAB.

Depending on the complexity of your project, choose the approach — scripting, functional, or object-oriented — that best enhances your code’s organization, readability, and efficiency.

2. Use argument validation to be explicit and break less code

The function below is a simple one with no argument validation.

In MATLAB, effective argument validation can significantly enhance your function’s robustness. There are three levels to consider:

  1. Do not use: A lot of if-clauses checking the function inputs. While straightforward, this will clutter your code.

2. If necessary, do use: inputParser built-in class

Available since 2007, you can parse the inputs and use validateattributes. This built-in class allows flexible input parsing and custom validation functions. While being useful, it can make your code less readable.

3. Do use: Input/Output Argument Blocks

Introduced in 2019, Input/Output Argument Blocks make it easy to handle data types, impose constraints and set defaults. They are more pleasant to read and more concise in comparison to the inputParser.

Imagine a function that takes a String input argument as the expression for a switct statement. Your switch statement has three possible cases and also one default case. Using argument validation, we can check the input at the beginning of the function, set a default and give the user precise feedback if there is a problem with the passed input argument — All with only one line of code. From a design perspective, we not only validate arguments but discourage using the varargin keyword because we define every input parameter explicitly.

Note: You can do the same thing for the output arguments. See the official documentation on function argument validation.

Summary: Use Input/Output Argument Blocks and be explicit, avoid using varargin, and use inputParser primarily for very exotic validation or legacy code where Argument Blocks are limited.

3. Use String instead of Chars

There is confusion regarding the usage of Char and String within MATLAB. Let’s first see how you define a variable as a Char Array or a String. I deliberately say Char “Array”, since you can think of it as a sequence of individual characters.

Thus, we can do indexing/slicing operations on Char Arrays like with a numeric Array.

In comparison, MATLAB treats a String as a single element, even within Arrays. Therefore, we are able to use Strings in Arrays like this.

Before Strings, Char Arrays were often wrapped in Cell Arrays for similar functionality. Note that the handling of Strings can be a bit different because we cannot use the indexing/slicing operations.

However, MATLAB already has built-in functions to manipulate Strings effectively which is easier to read and more versatile than Char Array manipulation.

Summary: Prefer the data type String (since 2016) over Char (Arrays) for their easier manageability and modern capabilities. Use MATLAB’s powerful String functions for efficient text handling. Strings are defined with double quotes “.

4. Use sprintf (formatted printing) instead of Char concatenation

This tip is closely related to the last section.

Let’s say we need to generate a String as a name for a file to be saved. Here is a rather verbose code to generate this formatted text using Char concatenation.

Is it that bad? It is working, right!? Well, what happens if we change the input and add an identifier with three fixed digits?

Switching to sprintf for String generation is a game-changer, especially if you’re accustomed to verbose Char concatenation methods, in just one line of code. If you use other programming languages too, you probably know sprintf already.

We would need to worry about the versatility and edge cases that might arise. Sprintf elegantly formats various data types into Strings, handling complexity and variability with ease.

Summary: Opt for sprintf to create Strings. It’s not only more robust and versatile but also significantly enhances the readability of your code, making String manipulations a breeze.

5. Start using “iterators”/single row vectors in “for” loops

MATLAB does not have a built-in iterator concept (or at least I haven’t found a default iterator semantic without writing my own class for this), however, we can cheat a bit to get a similar behaviour.

First some iterator background… In many programming languages, iterators are objects used to loop through collections. They enable step-by-step traversal, allowing the for keyword to process each element in the collection. Usually, I have seen for loops used as simple counters.

What happens if we want to loop over some specific Struct fields? The simple counter needs to be adjusted for Structs. However, the “iterator” method enhances code clarity and maintainability.

Still, it’s important to note that this technique is primarily applicable to flat Arrays. When working with other data types, like Cell Arrays or Structs, the for loop doesn’t operate as seamlessly as it might with a traditional iterator concept.

One crucial issue you might have noticed is the inability to modify original data directly in the approach we discussed. We are not able to modify our original data with this semantics because we do not have a reference to the original data. This behaviour is called “copy-on-write”. MATLAB lacks a mechanism to define a reference to a value/variable and is also not designed to do so! In the next section, you will learn how to make modifications to data without always using for loops.

Summary: Use a single row vector in conjunction with the for keyword for efficient traversal through values in flat Arrays. If modification of the original data is needed, see the next chapter which offers an alternative strategy.

6. Master arrayfun, structfun and cellfun

You might have already seen this concept used in some code or even used it yourself. The syntax might be a bit confusing to begin with and different to common MATLAB code. Despite seeming complex initially, these functions are incredibly useful. Why should you use them? Your code becomes more readable — once getting used to it :). But we will reflect on this in a second.

First, let me explain what these functions are used for. As the name suggests, they are used on a specific data type (Array, Struct or Cell) and will apply a function (fun) to them. Here are some code examples for each of them providing an overview.

To recap, we can apply a function to all values within a given data type. We can also parametrize the function handles that we supply. My personal favourite, which I often use to modify an Array of Structs, is the following.

Here, we use the arrayfun function because it is an Array of Structs and apply setfield on each scalar Struct. The cool thing is that the function allows for quick slicing operations on the Array of Structs and precisely controls modifications.

Summary: arrayfun, structfun and cellfun apply a function to their respective data type. Thus, we can avoid explicit loops and be more concise.

7. Embrace newer data types

MATLAB’s continuous updates have introduced new data structures that can enhance your day-to-day work. To clarify, when I mention “data structures” here, I’m referring to ways of organizing data, such as Structs or Cells, rather than basic data types like Integers, Floats or Chars. Two recent data structures are especially notable:

  • Table, available since 2013, can be valuable when handling tabular data and doing data analysis.

You will find more built-in functions that are specifically designed to handle tabular data (e.g. pivot just came out in the year 2023!)

  • Dictionary is the latest data structure and can be seen as a successor of container.Map. It is a data structure similar to Structs, where you have one or more fields (in Dictionaries called a key) and an assigned value to each of them. The Dictionary can be seen as a list of key-value pairs.

So then what is the difference to Structs? First, the values and also the keys can be any data type. Thus, we are not bound to assign names to our fields using a String/Char Array. Second, the keys are hashed items, resulting in faster lookups. Consequently, if you have many key-value pairs, consider using Dictionaries to improve code execution time.

Summary: Use Tables for tabular data and data analysis. Choose Dictionaries for dynamic, key-value pair-based data access with fast lookups. Keep using Structs for organized, field-based data storage where clarity and data structure are important.

8. Table data structure reigns supreme in data analysis

In the past years, Tables became the best data structure to work with data due to more built-in support. It has gotten easier to aggregate, filter and visualize data. In this section some common functions are showcased with an example table. Let’s start by reading a dataset and applying different concepts.

  • Data Aggregation: Functions like groupsummary allow for easy aggregation of data based on groups. You can calculate the mean, median, sum, or custom functions for grouped data.
  • Group Filtering: Using groupfilter, you can filter data based on conditions within groups, making it easier to analyze subsets of your data.
  • Data Manipulation: Tables support various data manipulations, including sorting, merging, and reshaping data, thus facilitating comprehensive data analysis.
  • Combining Data: You can join Tables based on common variables, enabling complex data integration tasks.
  • Visualization: Tables integrate well with MATLAB’s plotting functions, allowing for easy visualization of data for analysis and presentation purposes.

Summary: Tables as data structures are an essential tool for data analysis because they offer versatility and built-in support for aggregations, filtering and visualization.

9. Navigate the Nuances of Floating-Point Arithmetic

Floating-point arithmetic can lead to subtle errors in computations — a phenomenon intrinsic to how computers represent real numbers. Let’s take a look at the following classic example.

Such errors can impact badly designed conditions or can add up to significant errors in scientific computing where high precision is required. It is crucial to understand this concept, to not only write better code but also build precise systems if needed. So how can we tackle this problem?

First, we need to understand the machine epsilon (eps). This value represents the smallest difference between distinct floating-point numbers in MATLAB’s commonly used double-precision format (here is a good explanation with hex representation regarding eps and workings under the hood). Depending on your requirements, I recommend the following solutions in MATLAB.

  • For conditional checks: Use ismembertol with tolerance value eps. This method helps make accurate comparisons by accounting for floating-point discrepancies.
  • For high-precision scientific computing: Check out vpa (variable-precision arithmetic) or digits for calculations requiring high precision. This approach allows for more precise results, especially necessary in scientific computations.
  • For reduction in significant accumulation of round-off errors with doubles: Use round with a specific decimal precision to make sure to round to the expected number.

Summary: For conditional checks involving calculated values, ismembertol or a similar tolerance-based approach is recommended to ensure accuracy within the specified conditions. When higher precision is necessary for your calculations, consider using vpa. However, be aware that while vpa increases precision, it also significantly lengthens computation time. To reduce the accumulation of errors and keep a low computation time, round might be an option.

10. Handling missing data with “missing“

In MATLAB, we usually represent a missing number with NaN (Not a Number).

While NaN is used for checks, challenges arise when dealing with non-numeric data types like Strings. To address this, the missing type was introduced in 2017, extending the concept of NaN to other data types. The good news is, NaN stays NaN for numeric Arrays, so legacy code keeps working with the missing type. This extension is especially useful for data analysis and aggregations where we work with different data types. To efficiently handle missing values in such cases, use the missingflag parameter in aggregation functions.

Here are some behaviours of missing to keep in mind:

  • Numeric Arrays (Double, Single, Duration, calendarDuration) convert missing values to NaN.
  • Datetime Arrays translate missing to NaT (Not a Time).
  • Categorical Arrays treat missing as <undefined>.
  • In String Arrays, missing becomes <missing>.
  • Missing values are treated as distinct; e.g. unique will list each missing value.
  • Aggregations on arrays with missing values default to returning missing, use the missingflag parameter (e.g. omitnan or omitmissing) in aggregation functions to ignore missings.
  • From 2022 onwards, conditional checks like ismissing and anymissing are available, extending the functionality of isnan to other data types.

Summary: The missing type is the “better” NaN. It can handle the missing values of more data types in contrast to the legacy NaN. Missings are treated the same way in common numeric Arrays like NaN for backward compatibility (missing converts to NaN in this use case). Keep the behaviours of missing in mind!

11. Staying updated with MATLAB Release Notes

As you have seen in the past sections, we have gotten wonderful improvements in the form of more data types and tools to work with throughout the past few years. Each release brings new features and enhancements. Typically, two MATLAB versions are released each year, named after the year and a suffix with a or b. You can find the release notes here. They are clustered in subcategories. Check out the sections you are most interested in! Recent MATLAB releases have been a game-changer for me personally. The deployment of applications has gotten easier (more docker support) and advancements in data analysis functionalities have saved a lot of time.

Summary: Check out the release notes of new MATLAB releases. Most new functionalities make coding some advanced concepts way easier.

12. Effective Debugging with Conditional Breakpoints in MATLAB

Even with robust code design, encountering bugs is inevitable. MATLAB’s interpreted nature allows for immediate feedback through running specific lines. Thus, the whole program does not need to be compiled first, and I have often seen the use of immediate feedback by just running specific lines of code or the good old and reliable print statements. However, for more precise troubleshooting, I recommend utilizing MATLAB’s debugger.

The debugger allows you to examine the state of the program at the breakpoint. The big advantage is, that you can inspect every workspace variable present when the breakpoint is reached. Moreover, we can simply re-run sections and interactively explore the variables in the workspace within our debugger.

Nevertheless, in more complex applications your breakpoint might be stopping too early and your program is not in the desired state. To only trigger the breakpoint in a specific state (condition), you can right-click on the breakpoint and set a condition.

Conditional breakpoint at line4
MATLAB window to put in condition for a breakpoint

Alternatively, you can use dbstop in the command window.

Note: To automatically enter debugger mode when an error occurs, use the “Pause at error” option in MATLAB’s menu. This directs you to the exact line where the error is thrown, facilitating immediate debugging. However, be cautious, as the error might not always manifest as expected due to varying implementation details. To further enhance your error handling skills, familiarize yourself with functions like Mexception, throwAsCaller, rethrow, and throw, which offer nuanced ways to manage errors.

Summary: Choose the debugging approach that best suits your needs, be it through using the debugger, setting conditional breakpoints, or utilizing the “Pause at error” feature.

13. Start documenting like a pro (not the help function)

At last, you want to document your function so other users and your future self will understand the code. Good documentation starts with clean code and argument validation. Especially Argument Validation Blocks (see Section 2) explain input and output parameters in a clear fashion.

MATLAB’s ecosystem offers various ways to seek help and document code.

The most commonly used function help displays comments written directly after a function, serving as its documentation. However, there is no clear syntax on how to write the documentation (or at least I could not find any). Additionally, MATLAB’s publish function — particularly useful for .mlx live scripts — allows you to generate HTML outputs, sharing both your code and its output with others.

During my journey to write MATLAB code documentation, I was still not happy with these tools. I wanted to do something like this type of interactive documentation.

I dived deeper and found the solution in the Sphinx MATLAB extension. Thanks to all the contributors — you eased my work by a lot :).

To start using the extension, we require Python and install the package. Follow the instructions on their GitHub page for installation and usage.

The extension can parse your code comments and generate a fully functional HTML documentation page for you. Be mindful that it doesn’t autogenerate docstrings, so precision in formatting — including tabs, spaces, and punctuation — is crucial for optimal results.

Note that creating complete HTML documentation using the Sphinx MATLAB extension also involves writing RST files. While it is generally effective, be prepared for occasional parsing bugs due to unhandled edge cases. In my experience, you can work around it but I keep wondering whether I should start contributing and fixing these bugs ;)?

In one of my projects, I automated the documentation generation and integrated it with CI/CD pipelines for deployment to GitLab Pages. This process has not only streamlined our workflow but also raised awareness among my colleagues about the benefits of efficient documentation, testing and deployment practices. But this is a whole different story :).

Summary: Leverage the Python Sphinx MATLAB extension to get a functional HTML documentation page for your MATLAB application. You can easily host it on services like GitHub or GitLab. You’ll need to write the correct documentation syntax and RST files, but the effort pays off, especially for larger and more complex applications.

TLDR

  • [01] | Maintainability & Readability | Understand Scripts, Functions, Classes |
  • [02] | Maintainability & Readability | Use Argument Validation |
  • [03] | Text Handling | Strings Are Better Than Char Arrays |
  • [04] | Text Handling | Sprintf For Formatting Text |
  • [05] | Looping | Use ‘iterators’ Instead Of Simple Counters |
  • [06] | Looping | Apply Functions To Each Element (arrayfun, cellfun, structfun) |
  • [07] | Datatypes | Use Tables And Dictionaries If Needed |
  • [08] | Data Analysis | Tables For Data Analysis |
  • [09] | Calculation Errors | Handling Precision Correctly Depending On Use Case |
  • [10] | Missing | Use Missings over NaN |
  • [11] | Education | Stay Up To Date |
  • [12] | Debugging | Conditional Breakpoints |
  • [13] | Documentation | Produce A HTML Documentation In Python |

I hope you find these MATLAB tips useful in your upcoming projects. I’m keen to hear how they work for you, so feel free to share your feedback and experiences!

--

--

Schoepfloeffel

Passionate Humboldtian with a keen interest in the world of programming and statistics.