Convert cell array to structure array
structArray =
cell2struct(cellArray, fields, dim)
creates
a structure array, structArray =
cell2struct(cellArray, fields, dim)structArray, from the
information contained within cell array cellArray.
The fields argument specifies field names for the structure array.
This argument is a character array, a cell array of character vectors, or a string
array.
The dim argument tells MATLAB® which
axis of the cell array to use in creating the structure array. Use
a numeric double to specify dim.
To create a structure array with fields derived from N rows
of a cell array, specify N field names in the fields argument,
and the number 1 in the dim argument. To
create a structure array with fields derived from M columns
of a cell array, specify M field names in the fields argument
and the number 2 in the dim argument.
The structArray output is a structure
array with N fields, where N is
equal to the number of fields in the fields input
argument. The number of fields in the resulting structure must equal
the number of cells along dimension dim that
you want to convert.
Create the following table for use with the examples in this section. The table lists information about the employees of a small Engineering company. Reading the table by rows shows the names of employees by department. Reading the table by columns shows the number of years each employee has worked at the company.
| 5 Years | 10 Years | 15 Years | |
|---|---|---|---|
| Development | Lee, Reed, Hill | Dean, Frye | Lane, Fox, King |
| Sales | Howe, Burns | Kirby, Ford | Hall |
| Management | Price | Clark, Shea | Sims |
| Quality | Bates, Gray | Nash | Kay, Chase |
| Documentation | Lloyd, Young | Ryan, Hart, Roy | Marsh |
Enter the following commands to create the initial cell array employees:
devel = {{'Lee','Reed','Hill'}, {'Dean','Frye'}, ...
{'Lane','Fox','King'}};
sales = {{'Howe','Burns'}, {'Kirby','Ford'}, {'Hall'}};
mgmt = {{'Price'}, {'Clark','Shea'}, {'Sims'}};
qual = {{'Bates','Gray'}, {'Nash'}, {'Kay','Chase'}};
docu = {{'Lloyd','Young'}, {'Ryan','Hart','Roy'}, {'Marsh'}};
employees = [devel; sales; mgmt; qual; docu]
employees =
{1x3 cell} {1x2 cell} {1x3 cell}
{1x2 cell} {1x2 cell} {1x1 cell}
{1x1 cell} {1x2 cell} {1x1 cell}
{1x2 cell} {1x1 cell} {1x2 cell}
{1x2 cell} {1x3 cell} {1x1 cell}This is the resulting cell array:

Convert the cell array to a struct along dimension 1:
Convert the 5-by-3 cell array along its first dimension to construct a 3-by-1 struct array with 5 fields. Each of the rows along dimension 1 of the cell array becomes a field in the struct array:

Traversing the first (i.e., vertical) dimension, there are 5 rows with row headings that read as follows:
rowHeadings = {'development', 'sales', 'management', ...
'quality', 'documentation'};
Convert the cell array to a struct array, depts,
in reference to this dimension:
depts = cell2struct(employees, rowHeadings, 1)
depts =
3x1 struct array with fields:
development
sales
management
quality
documentation
Use this row-oriented structure to find the names of the Development staff who have been with the company for up to 10 years:
depts(1:2).development
ans =
'Lee' 'Reed' 'Hill'
ans =
'Dean' 'Frye'Convert the same cell array to a struct along dimension 2:
Convert the 5-by-3 cell array along its second dimension to construct a 5-by-1 struct array with 3 fields. Each of the columns along dimension 2 of the cell array becomes a field in the struct array:

Traverse the cell array along the second (or horizontal) dimension. The column headings become fields of the resulting structure:
colHeadings = {'fiveYears' 'tenYears' 'fifteenYears'};
years = cell2struct(employees, colHeadings, 2)
years =
5x1 struct array with fields:
fiveYears
tenYears
fifteenYears
Using the column-oriented structure, show how many employees from the Sales and Documentation departments have worked for the company for at least 5 years:
[~, sales_5years, ~, ~, docu_5years] = years.fiveYears
sales_5years =
'Howe' 'Burns'
docu_5years =
'Lloyd' 'Young'
Convert only part of the cell array to a struct:
Convert only the first and last rows of the cell array. This results in a 3-by-1 struct array with 2 fields:
rowHeadings = {'development', 'documentation'};
depts = cell2struct(employees([1,5],:), rowHeadings, 1)
depts =
3x1 struct array with fields:
development
documentation
Display those employees who belong to these departments for all three periods of time:
for k=1:3
depts(k,:)
end
ans =
development: {'Lee' 'Reed' 'Hill'}
documentation: {'Lloyd' 'Young'}
ans =
development: {'Dean' 'Frye'}
documentation: {'Ryan' 'Hart' 'Roy'}
ans =
development: {'Lane' 'Fox' 'King'}
documentation: {'Marsh'}cell | cell2table | fieldnames | iscell | isstruct | struct | struct2cell | table2struct