Main Content

dataset2struct

Class: dataset

(Not Recommended) Convert dataset array to structure

The dataset data type is not recommended. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information.

Syntax

S = dataset2struct(D)
S = dataset2struct(D,'AsScalar',true)

Description

S = dataset2struct(D) converts a dataset array to a structure array. Each variable of D becomes a field in S. If D is an M-by-N dataset array, then S is M-by-1 and has N fields. If D contains observation names, then S contains those names in the additional field ObsNames.

S = dataset2struct(D,'AsScalar',true) converts a dataset array to a scalar structure. Each variable of D becomes a field in S. If D is an M-by-N dataset array, then S has N fields, each of which as M rows. If D contains observation names, then S contains those names in the additional field ObsNames.

Input Arguments

D

M-by-N dataset array.

Output Arguments

S

M-by-1 structure array, with N fields. If the input dataset array contains observation names, then S has an additional field ObsNames.

If you specify 'AsScalar',true, then S is a scalar structure, with N fields, each with M rows.

Examples

expand all

Load sample dataset array.

load('hospital')

Create a dataset array, D, that has only a subset of the observations and variables.

D = hospital(1:8,{'LastName','Sex','Age'});
size(D)
ans = 1×2

     8     3

The dataset array D has 8 observations and 3 variables.

Convert D to a structure array.

S = dataset2struct(D)
S=8×1 struct array with fields:
    ObsNames
    LastName
    Sex
    Age

The structure is 8x1, corresponding to the 8 observations in the dataset array. S also has the field ObsNames, since D had observation names.

Display the field data for the first element of S.

S(1)
ans = struct with fields:
    ObsNames: 'YPL-320'
    LastName: 'SMITH'
         Sex: Male
         Age: 38

This information corresponds to the first observation (row) of the dataset array.

Load sample dataset array.

load('hospital')

Create a dataset array, D, that has only a subset of the observations and variables.

D = hospital(1:8,{'LastName','Sex','Age'});
size(D)
ans = 1×2

     8     3

The dataset array D has 8 observations and 3 variables.

Convert D to a scalar structure array.

S = dataset2struct(D,'AsScalar',true)
S = struct with fields:
    ObsNames: {8x1 cell}
    LastName: {8x1 cell}
         Sex: [8x1 nominal]
         Age: [8x1 double]

The data in the fields of the scalar structure is 8x1, corresponding to the 8 observations in the dataset array. S also has the field ObsNames, since D had observation names.

Display the data for the field LastName.

S.LastName
ans = 8x1 cell
    {'SMITH'   }
    {'JOHNSON' }
    {'WILLIAMS'}
    {'JONES'   }
    {'BROWN'   }
    {'DAVIS'   }
    {'MILLER'  }
    {'WILSON'  }

The structure field LastName contains all of the data that was in the original dataset array variable, LastName.