MATLAB R2019b table variable names can have spaces

On the 11th of September MATLAB R2019b was released. There are countless new features to MATLAB and all the toolboxes. This article will focus on a personal favorite of Gareth, one of the co-founders of VersionBay: in MATLAB R2019b table variable names can have spaces and non-ASCII characters. In the MATLAB Release Notes it appears as:

table and timetable Data Types: Variable names can have any characters, including spaces and non-ASCII characters

MATLAB Release Note for R2019b

For many years, it has been tricky to work with spreadsheets with “special” column names. When you used readtable in MATLAB, the column names were automatically converted into MATLAB table variable names, which removed spaces and changed non-ASCII characters. This approach is understandable but given that Gareth is Portuguese and many categories or names in Portuguese have accents, it was a little sad that it was not possible to use them directly when writing code. For example, País means country, and MATLAB would convert this to Pa_s, which makes code less readable. So most people end up changing the variable name to Pais without the accent, however, the word Pais means parents. So the code and data could still look confusing.

Example of a spreadsheet with Countries written in Portuguese

Before R2019b

Notice the warning: “Table variables were modified to make them valid MATLAB identifiers.”

In R2019b

There is a new name-value pair argument: “PreserveVariableNames”

This is not a huge new feature but certainly appreciated. There are a few considerations that MathWorks calls out about using it, however, a key aspect is that it only works from R2019b onwards.

% R2019a
data = readtable("SimpleTable.xlsx")

% R2019b
data = readtable("SimpleTable.xlsx",  ...
    "PreserveVariableNames", true)


If you found this article interesting and would like to learn more tips and tricks Contact Us.