NOTE: This is the eighth in my series of “10 things I wish I had known when I started using MATLAB”.
MATLAB has a wide variety of variable types available, but one that took me an embarassingly long time to learn about and appreciate is the cell array
.
The cell array
is one of the most flexible ways to store data because it can contain data of different types. So, a cell array
could contain a text string, a number, a matrix, and even another cell array!
Here’s an example of a cell array
:
cell_array = {"Douglas", 48, [1,0;0,1]};
You can address elements of a cell array numerically by their position. For example, cell_array{1}
returns "Douglas"
.
Note the use of “{ }” brackets denoting the cell array and distinguishing it from regular arrays (which are defined using “[ ]” brackets and are referenced using “( )” brackets).
Now, to be 100% correct, you can use “( )” to address cells - you’ll just get a subset of the cell array back, not the values of those elements in the cell array. Confusing? Here’s another example: if I type cell_array(1)
I get {"Douglas"}
back. See how what was returned was a cell array, itself?
Read more about cell arrays in the official MathWorks documentation.