Skip to content

Exercise 7.5 alternate solution: A deep dive into MATLAB patterns #112

@BoschiGiacomo

Description

@BoschiGiacomo

I'm writing this post because I have found a MATLAB feature that I would like to share with you.

Alternate Solution:

The exercise asks to create a correlation matrix from the head.mat data, display it as a table, and shorten the variable names, because the original names are too long to read comfortably in a table view. The rule is to keep only the part of each name after the second underscore.

The book’s solution splits the variable names on "_", producing an n x 3 string array, and then uses the third column as the new variable names. Instead, it is possible to do this in one step using pattern objects and regular expressions.

Since I like to write reusable and flexible code, I came up with an alternate approach using patterns.

Here is a part of my solution to the exercise:

% load data
load head.mat
Xtab= head;
clear head

VarNames= Xtab.Properties.VariableNames;
pat= lettersPattern + "_" + lettersPattern + "_";
VarNames= extractAfter(VarNames,pat);

X= Xtab{:,:};

[R, Pval]= corr(X);
Rtab= array2table(round(R,2), "RowNames", VarNames, "VariableNames", VarNames);

The key line is the pattern definition:

pat= lettersPattern + "_" + lettersPattern + "_";

This matches a sequence of letters, followed by an underscore, another sequence of letters, and another underscore. Using extractAfter(VarNames, pat) then returns everything that comes after this pattern, which is exactly the “short” variable name required by the exercise.

Here, pat is a pattern object that stores the text pattern to search for in the variable names. It is very flexible because it allows searching for word patterns without knowing the exact text in advance.

Why is this useful?

For this exercise, the book’s solution works perfectly, because all variable names follow the same convention with exactly two underscores. In other situations, variable names might be less regular, and it can be convenient to express the structure you expect using patterns. A pattern-based approach like the one above makes it easier to adapt the code if naming conventions change later on.

Learn more:

For more details on pattern objects and text processing in MATLAB, see the official documentation on pattern, building pattern expressions, and extractAfter

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions