cell2dataset

Convert cell array to dataset array

The dataset data type might be removed in a future release. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information.

Syntax

Description

example

ds = cell2dataset(C) converts a cell array to a dataset array.

example

ds = cell2dataset(C,Name,Value) performs the conversion using additional options specified by one or more Name,Value pair arguments.

Examples

expand all

Convert Cell Array to Dataset Array

Convert a cell array to a dataset array using the default options.

Create a cell array to convert.

C = {'Name','Gender','SystolicBP','DiastolicBP';
     'CLARK','M',124,93;
     'BROWN','F',122,80;
     'MARTIN','M',130,92}
C = 

    'Name'      'Gender'    'SystolicBP'    'DiastolicBP'
    'CLARK'     'M'         [       124]    [         93]
    'BROWN'     'F'         [       122]    [         80]
    'MARTIN'    'M'         [       130]    [         92]

Convert the cell array to a dataset array.

ds = cell2dataset(C)
ds = 

    Name            Gender     SystolicBP    DiastolicBP
    'CLARK'         'M'        124           93         
    'BROWN'         'F'        122           80         
    'MARTIN'        'M'        130           92         

The first row of C become the variable names in the output dataset array, ds.

Create a Dataset Array with Multicolumn Variables

Convert a cell array to a dataset array containing multicolumn variables.

Create a cell array to convert.

C = {'Name','Gender','SystolicBP','DiastolicBP';
     'CLARK','M',124,93;
     'BROWN','F',122,80;
     'MARTIN','M',130,92}
C = 

    'Name'      'Gender'    'SystolicBP'    'DiastolicBP'
    'CLARK'     'M'         [       124]    [         93]
    'BROWN'     'F'         [       122]    [         80]
    'MARTIN'    'M'         [       130]    [         92]

Convert the cell array to a dataset array, combining the systolic and diastolic blood pressure measurements into one variable named BloodPressure.

ds = cell2dataset(C,'NumCols',[1,1,2]);
ds.Properties.VarNames{3} = 'BloodPressure';
ds
ds = 

    Name            Gender     BloodPressure  
    'CLARK'         'M'        124          93
    'BROWN'         'F'        122          80
    'MARTIN'        'M'        130          92

The output dataset array has three observations and three variables.

Input Arguments

expand all

C — Input cell arraycell array

Input cell array to convert to a dataset array, specified as an M-by-N cell array. Each column of C becomes a variable in the output dataset array, ds. By default, cell2dataset assumes that the first row of C contains variable names.

Data Types: cell

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'ReadVarNames',false,'ReadObsNames',true specifies that the first row of the cell array does not contain variable names, but the first column contains observation names.

'ReadVarNames' — Indicator for whether or not to read variable namestrue (default) | false

Indicator for whether or not to read variable names from the first row of the input cell array, specified as the comma-separated pair consisting of 'ReadVarNames' and either true or false. The default value is true, unless variable names are specified using the name-value pair argument VarNames. When ReadVarNames is false, cell2dataset creates default variable names if you do not provide any.

Example: 'ReadVarNames',false

'VarNames' — Variable names for output dataset arraycell array of strings

Variable names for the output dataset array, specified as the comma-separated pair consisting of 'VarNames' and a cell array of strings. You must provide a variable name for each variable in ds. The names must be valid MATLAB identifiers, and must be unique.

Example: 'VarNames',{'myVar1','myVar2','myVar3'}

'ReadObsNames' — Indicator for whether or not to read observation namesfalse (default) | true

Indicator for whether or not to read observation names from the input cell array, specified as the comma-separated pair consisting of 'ReadObsNames' and either true or false. When ReadObsNames has the value true, cell2dataset creates observation names in ds using the first column of C, and sets ds.Properties.DimNames equal to {C{1,1},'Variables'}.

Example: 'ReadObsNames',true

'ObsNames' — Observation names for output dataset arraycell array of strings

Observation names for the output dataset array, specified as the comma-separated pair consisting of 'ObsNames' and a cell array of strings. The names do not need to be valid MATLAB identifiers, but they must be unique.

'NumCols' — Number of columns for each variablevector of nonnegative integers

Number of columns for each variable in ds, specified as the comma-separated pair consisting of 'NumCols' and a vector of nonnegative integers. When the number of columns for a variable is greater than one, cell2dataset combines multiple columns in C into a single variable in ds. The vector you assign to NumCols must sum to size(C,2), or size(C,1) of ReadObsNames is equal to true.

For example, to convert a cell array with eight columns into a dataset array with five variables, specify a vector with five elements that sum to eight, such as 'NumCols',[1,1,3,1,2].

Output Arguments

expand all

ds — Output dataset arraydataset array

Output dataset array, returned by default with a variable for each column of C, an observation for each row of C (except for the first row), and variable names corresponding to the first row of C.

  • If you set ReadVarNames equal to false (or specify VarNames), then there is an observation in ds for each row of C, and cell2dataset creates default variable names (or uses the names in VarNames).

  • If you set ReadObsNames equal to true, then cell2dataset uses the first column of C as observation names.

  • If you specify NumCols, then the number of variables in ds is equal to the length of the specified vector of column numbers.

More About

Was this topic helpful?