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
Examples
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=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
.
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=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
C
— Input cell array
cell 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
| 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.
ReadVarNames
— Indicator for whether or not to read variable names
true
(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 array
string array | cell array of character vectors
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'}
ReadObsNames
— Indicator for whether or not to read observation names
false
(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 array
string array | cell array of character vectors
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.
NumCols
— Number of columns for each variable
vector 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
ds
— Output dataset array
dataset 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 tofalse
(or specifyVarNames
), then there is an observation inds
for each row ofC
, andcell2dataset
creates default variable names (or uses the names inVarNames
).If you set
ReadObsNames
equal totrue
, thencell2dataset
uses the first column ofC
as observation names.If you specify
NumCols
, then the number of variables inds
is equal to the length of the specified vector of column numbers.
Version History
Introduced in R2012b
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)