# MATLAB structures and tables

Although this isn’t directly about making figures in MATLAB, many of the powerful figuresmithing tricks that I use (and will describe in future posts) work best if your data is organised in a table or structure. So, it’s worth having a think about what these variable types are, and how to use and construct them.

## Structures are like subfolders in your Workspace

When you create a variable in MATLAB it appears in your Workspace and you can then do things with it. A structure is simply a container for a bunch of other variables (called “fields”), that can all be totally unrelated to each other, and different types and sizes.

Fields are created, and then accessed, using a dot. Try it out!

``````% Create some variables and do some stuff
x = 2;
y = 3;
z = x + y;
word = 'hello';
disp(word)

% Do the same thing, but with everything in a structure
myStruct.x = 2;
myStruct.y = 3;
myStruct.z = myStruct.x + myStruct.y;
myStruct.word = 'hello';
disp(myStruct.word)
``````

Double click on the variable `myStruct` that appears in your workspace to have a look at its contents.

## Tables are structures with special rules

A table is very similar to a structure, except that the fields in a table (now called columns) all have to have the same number of rows as each other. In other words…

If you have some data that’s from or could sensibly be put in a spreadsheet, a table is probably your best way to work with it in MATLAB. Tables allow you to slice and dice the data in many more useful ways than you could with the same data in a structure.

How do we make a table? There’s a few ways. If you have a structure that obeys the rules of tables, you can simply convert with `struct2table`:

``````% Create structure fields
clear myTable
myTable.x = [1; 2; 3; 4];
myTable.y = [3; 9; 8; 2];

% Convert structure to table
myTable = struct2table(myTable);

% Make a new column z with the sum of x and y
myTable.z = myTable.x + myTable.y;
% Make a new table with only rows where y < 5
L = myTable.y < 5;
myTable2 = myTable(L, :);

% Make a new table with only columns y and z
myTable3 = myTable(:, {'y' 'z'});
``````

(Why did we `clear myTable` at the top? The function `struct2table` has a behaviour that’s quite a pain in the posterior: if you input something that’s already a table, it throws an error and stops your code. So this clear just prevents that from happening.)

Alternatively, if you have the data in a spreadsheet file, you can just use `readtable` to directly import it as a table. This has a bunch of settings that you can read all about by typing `readtable` into your MATLAB Editor or Command Window and pressing F1.

## Tables and structures allow dynamic referencing

One of the most powerful reasons to use tables and structures, rather than just normal variables, is that you can “dynamically reference” the fields within them. This means that you can access the fields within them with the name of the field being generated by some other code, instead of you having to write it out explicitly. In future posts back on the subject of figuresmithing, we will use this feature extensively.

But for now, this is perhaps best illustrated with a quick example. We have some data in an matrix and we want to move each column into its own variable with a specific name:

``````% Create original matrix
mx = [1 2 3; 5 6 3; 9 2 1; 4 8 2];

% Approach 1: put columns into different variables
var1 = mx(:, 1);
var2 = mx(:, 2);
var3 = mx(:, 3);

% Approach 2: put columns into different fields of a structure
sfields = {'var1' 'var2' 'var3'};
for F = 1:numel(sfields)
st.(sfields{F}) = mx(:, F);
end % for F
``````

In other words, you can create and then the field called var1 in the structure `st` either using `st.var1` or `st.('var1')`. The latter option gives us an enormous amount of flexibility that you may already be able to see, but if not then keep an eye out for future posts where we will develop the idea further.

I have encountered quite a few people who found themselves wanting to dynamically assign variable names. Often the solution they arrive upon is to use the eval function. However, this approach is strongly discouraged. Stick to structures and tables instead!

## In summary

• Structures are like subfolders in your MATLAB Workspace;
• Tables are a special type of structure, for when the data look like a spreadsheet;
• Structures and tables allow “dynamic referencing” of the fields within them, which is essentially a superpower;
• We will use these data types extensively in making figures in later posts.