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 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
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
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
ReadVarNamesequal tofalse(or specifyVarNames), then there is an observation indsfor each row ofC, andcell2datasetcreates default variable names (or uses the names inVarNames).If you set
ReadObsNamesequal totrue, thencell2datasetuses the first column ofCas observation names.If you specify
NumCols, then the number of variables indsis 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.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)