mat2dataset
(Not Recommended) Convert matrix 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 Matrix to Dataset Array
Convert a matrix to a dataset array using the default options.
Load sample data.
load('fisheriris')
X = meas;
size(X)
ans = 1×2
150 4
Convert the matrix to a dataset array.
ds = mat2dataset(X); size(ds)
ans = 1×2
150 4
ds(1:5,:)
ans = X1 X2 X3 X4 5.1 3.5 1.4 0.2 4.9 3 1.4 0.2 4.7 3.2 1.3 0.2 4.6 3.1 1.5 0.2 5 3.6 1.4 0.2
When you do not specify variable names, mat2dataset
uses the matrix name and column numbers to create default variable names.
Convert Matrix to Dataset Array with Variable Names
Load sample data.
load('fisheriris')
X = meas;
size(X)
ans = 1×2
150 4
Convert the matrix to a dataset array, providing a variable name for each of the four column of X
.
ds = mat2dataset(X,'VarNames',{'SLength',... 'SWidth','PLength','PWidth'}); size(ds)
ans = 1×2
150 4
ds(1:5,:)
ans = SLength SWidth PLength PWidth 5.1 3.5 1.4 0.2 4.9 3 1.4 0.2 4.7 3.2 1.3 0.2 4.6 3.1 1.5 0.2 5 3.6 1.4 0.2
Create a Dataset Array with Multicolumn Variables
Convert a matrix to a dataset array containing multicolumn variables.
Load sample data.
load('fisheriris')
X = meas;
size(X)
ans = 1×2
150 4
Convert the matrix to a dataset array, combining the sepal measurements (the first two columns) into one variable named SepalMeas
, and the petal measurements (third and fourth columns) into one variable names PetalMeas
.
ds = mat2dataset(X,'NumCols',[2,2],... 'VarNames',{'SepalMeas','PetalMeas'}); ds(1:5,:)
ans = SepalMeas PetalMeas 5.1 3.5 1.4 0.2 4.9 3 1.4 0.2 4.7 3.2 1.3 0.2 4.6 3.1 1.5 0.2 5 3.6 1.4 0.2
The output dataset array has 150 observations and 2 variables.
size(ds)
ans = 1×2
150 2
Input Arguments
X
— Input matrix
matrix
Input matrix to convert to a dataset array, specified as an
M-by-N numeric matrix. Each column
of X
becomes a variable in the output
M-by-N dataset array.
Data Types: single
| double
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: 'NumCols',[1,1,2,1]
specifies that the 3rd and 4th
columns of the input matrix should be combined into a single
variable.
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'}
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,
mat2dataset
combines multiple columns in
X
into a single variable in
ds
. The vector you assign to
NumCols
must sum to
size(X,2)
.
For example, to convert a matrix 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
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)