Day 27: Conversion functions and how to use them well

As you acquire, store and analyze data, you will likely encounter many different variable types and data structures in MATLAB. These different data types may appear in your MATLAB workspace as the product of built-in or custom MATLAB functions, but you may also find that for some of your data, cells make sense whereas in other cases you’d turn to a table or structure. It’s great to try and think ahead about what will work best for you. But fortunately there are dozens of conversion functions, and today I’ll be documenting some of the most commonly-used ones.

In general, almost all variable types can be turned into another type. Here is a chart showing all the different directions you can go by simply placing a “2” in between the variable types. For example table2array, cell2struct, etc. are all valid functions in MATLAB.

The nice thing about the conversion functions is that most of them will work with just one input: the thing you want to convert. However, there are some caveats. When converting structures to arrays or cells, you lose information about your structure field names. The same goes for tables that are converted into arrays or cells. Here’s a more detailed table that contains all the information you need to go between the different 2 functions.

And below are a couple of use cases for the functions I rely on heavily:

cell2mat: If I have irregularly sized numerical data within a cell array and I want to extract a summary statistic from each cell. Then convert the summary statistics into a matrix.

cell_data = { rand(10,2); rand(5,2)};
centroids = cellfun(@(x) mean(x,1), cell_data)
centroids_mat = cell2mat( centroids )
The first row is the first output of the cellfun.

struct2table: Many of MATLAB’s built-in analysis functions (e.g. regionprops) will output a structure. Structures, however, are hard to filter, so turning them into a table gets you to the point where you can filter the outputs of the initial function. The nice thing about this function is that it takes the structure field names and converts them straight into variables so you’ll never miss a beat.

struct2array: The structure analog of cell2mat — use it only if your structure only contains data of the same type.

mystructure = struct();
mystructure.Day1 = 1;
mystructure.Day2 = 2;
mystructure.Day3 = 3;
output = struct2array( mystructure )

Note that the code below would not work as you intend:

mystructure = struct();
mystructure.Day1 = 1;
mystructure.Day2 = 2;
mystructure.Day3 = 3;
mystructure.Label = 'Mouse';
output = struct2array( mystructure )

Hope this gives you some clarity about which of the “2” functions you’ll need to add to your analysis pipeline, and which make the most sense for you!

Neuroscientist and data scientist at Columbia University. On Twitter: @NeuroJoJo