Main Content

cell2struct

셀형 배열(Cell Array)을 구조체형 배열(Structure Array)로 변환

구문

structArray = cell2struct(cellArray, fields, dim)

설명

structArray = cell2struct(cellArray, fields, dim)은 셀형 배열 cellArray에 포함된 정보를 사용하여 구조체형 배열 structArray를 만듭니다.

fields 인수는 구조체형 배열의 필드 이름을 지정합니다. 이 인수는 문자형 배열, 문자형 벡터로 구성된 셀형 배열 또는 string형 배열입니다.

dim 인수는 구조체형 배열을 만들 때 사용할 셀형 배열의 축 정보를 MATLAB®에 전달합니다. 숫자형 double을 사용하여 dim을 지정합니다.

셀형 배열의 N개 행에서 파생된 필드를 갖는 구조체형 배열을 만들려면 fields 인수에 N 필드 이름을 지정하고 dim 인수에 숫자 1을 지정합니다. 셀형 배열의 M개 열에서 파생된 필드를 갖는 구조체형 배열을 만들려면 fields 인수에 M 필드 이름을 지정하고 dim 인수에 숫자 2를 지정합니다.

structArray 출력값은 필드가 N개인 구조체형 배열입니다. 여기서 Nfields 입력 인수의 필드 개수와 동일합니다. 결과 구조체의 필드 개수는 변환할 차원 dim을 따른 셀의 개수와 동일해야 합니다.

예제

이 섹션의 예제에 사용할 다음 표를 만듭니다. 이 표는 소규모 엔지니어링 회사의 직원 정보를 제공합니다. 표의 행에는 각 부서의 직원 이름이 있습니다. 표의 열은 각 직원이 해당 회사에서 근속한 연수를 보여줍니다.

 5년10년15년
개발부(Development)Lee, Reed, HillDean, FryeLane, Fox, King
영업부(Sales)Howe, BurnsKirby, FordHall
관리부(Management)PriceClark, SheaSims
품질 관리부(Quality)Bates, Gray NashKay, Chase
문서 관리부(Document)Lloyd, YoungRyan, Hart, RoyMarsh

다음 명령을 입력하여 초기 셀형 배열 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}

생성된 셀형 배열은 다음과 같습니다.

5 by 3 cell array of employees

셀형 배열의 1차원을 따라 구조체로 변환:

  1. 5×3 셀형 배열을 1차원을 따라 변환하여 필드가 5개인 3×1 구조체형 배열을 생성합니다. 1차원을 따라 셀형 배열의 각 행은 구조체형 배열에서 하나의 필드가 됩니다.

    5 by 3 cell array converted to 3 by 1 struct array

    첫 번째(세로) 차원을 순회하면 다음과 같은 행 제목을 가진 5개의 행이 존재하게 됩니다.

    rowHeadings = {'development', 'sales', 'management', ...
       'quality', 'documentation'};
    
  2. 이 차원을 참조하여 셀형 배열을 구조체형 배열 depts로 변환합니다.

    depts = cell2struct(employees, rowHeadings, 1)
    depts = 
    3x1 struct array with fields:
        development
        sales
        management
        quality
        documentation
    
  3. 이 행 방향 구조체를 사용하여 회사에 10년 이하 근속한 개발부(Development) 직원의 이름을 찾습니다.

    depts(1:2).development
    ans = 
        'Lee'    'Reed'    'Hill'
    ans = 
        'Dean'    'Frye'

동일한 셀형 배열을 2차원을 따라 구조체로 변환:

  1. 5×3 셀형 배열을 2차원을 따라 변환하여 필드가 3개인 5×1 구조체형 배열을 생성합니다. 셀형 배열의 2차원을 따라 각 열은 구조체형 배열의 필드가 됩니다.

    5 by 3 cell array converted to 5 by 1 struct array

  2. 2차원(또는 가로)을 따라 셀형 배열을 순회합니다. 열 제목이 결과 구조체의 필드가 됩니다.

    colHeadings = {'fiveYears' 'tenYears' 'fifteenYears'};
    
    years = cell2struct(employees, colHeadings, 2)
    years = 
    5x1 struct array with fields:
        fiveYears
        tenYears
        fifteenYears
    
  3. 열 방향 구조체를 사용하여 회사에 5년 이상 근속한 영업부(Sales) 및 문서 관리부(Document) 소속 직원을 표시합니다.

    [~, sales_5years, ~, ~, docu_5years] = years.fiveYears
    sales_5years = 
        'Howe'    'Burns'
    docu_5years = 
        'Lloyd'    'Young'
    

셀형 배열의 일부만 구조체로 변환:

  1. 셀형 배열의 첫 번째 행과 마지막 행만 변환합니다. 그 결과 2개의 필드를 갖는 3×1 구조체형 배열이 생성됩니다.

    rowHeadings = {'development', 'documentation'};
    
    depts = cell2struct(employees([1,5],:), rowHeadings, 1)
    depts = 
    3x1 struct array with fields:
        development
        documentation

    5 by 3 cell array converted to 3 by 1 struct array

  2. 3개 모두의 기간에 대해 이들 부서에 근속한 직원을 표시합니다.

    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 이전에 개발됨