Main Content

cell2dataset

(Not Recommended) Convert cell array to dataset array

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.

Description

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.

example

Examples

collapse all

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=4×4 cell array
    {'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.

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=4×4 cell array
    {'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

collapse all

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 | string

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

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.

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

Variable names for the output dataset array, specified as the comma-separated pair consisting of 'VarNames' and a string array or cell array of character vectors. 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'}

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

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

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

collapse all

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.

Version History

Introduced in R2012b