Documentation

cell2struct

Convert cell array to structure array

Syntax

structArray= cell2struct(Cellarray,fields,dim)

Description

structArray= cell2struct(Cellarray,fields,dim)creates a structure array,structArray,从单元阵列中包含的信息Cellarray.

Thefields参数指定结构数组的字段名称。该参数是字符数组或字符向量的单元格数组。

Thedimargument tells MATLAB®which axis of the cell array to use in creating the structure array. Use a numericdouble指定dim.

创建一个结构数组,并带有从Nrows of a cell array, specifyN字段名称fieldsargument, and the number 1 in thedim争论。创建一个结构数组,并带有从Mcolumns of a cell array, specifyM字段名称fields参数和数字2dim争论。

ThestructArray输出是一个结构数组Nfields, whereNis equal to the number of fields in thefields输入参数。所得结构中的字段数必须等于沿尺寸的单元格数dimthat you want to convert.

Examples

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年 10 Years 15年
Development 李,里德,希尔 Dean, Frye Lane, Fox, King
Sales Howe, Burns 柯比,福特 Hall
管理 价格 克拉克,乳木 Sims
Quality Bates, Gray 纳什 凯,蔡斯
Documentation Lloyd, Young Ryan, Hart, Roy Marsh

Enter the following commands to create the initial cell arrayemployees:

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:

将单元阵列转换为沿尺寸1的结构:

  1. 将5 x-3单元格沿其第一个维数转换为具有5个字段的3 by-1结构阵列。沿单元阵列的每个行沿尺寸1的每个行变成结构阵列中的一个字段:

    遍历第一个(即垂直)维度,有5行带有排名,如下所示:

    rowHeadings = {'development', 'sales', 'management', ... 'quality', 'documentation'};
  2. 细胞数组转换成一个结构体数组,部门, in reference to this dimension:

    部门= cell2struct(employees, rowHeadings, 1) depts = 3x1 struct array with fields: development sales management quality documentation
  3. Use this row-oriented structure to find the names of the Development staff who have been with the company for up to 10 years:

    部门(1:2).Development ans ='Lee'Reed''Hill'Ans ='Dean''Frye'

将相同的单元阵列转换为沿尺寸2的结构:

  1. 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:

  2. Traverse the cell array along the second (or horizontal) dimension. The column headings become fields of the resulting structure:

    colheadings = {'五年''tenyears''fifteenears'};年= Cell2truct(员工,Colheadings,2)年= 5x1结构阵列:五年tenyears十五年
  3. 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] =年。

Convert only part of the cell array to a struct:

  1. 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

  2. 在所有三个时期内显示那些属于这些部门的员工:

    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'}

在R2006a之前引入

这个话题有帮助吗?