Documentation

mat2dataset

Convert matrix to dataset array

Thedatasetdata type might be removed in a future release. To work with heterogeneous data, use the MATLAB®tabledata type instead. See MATLABtabledocumentation for more information.

Syntax

ds = mat2dataset(X)
ds = mat2dataset(X,Name,Value)

Description

example

ds= mat2dataset(X)converts a matrix to adatasetarray.

example

ds= mat2dataset(X,Name,Value)performs the conversion using additional options specified by one or moreName,Valuepair arguments.

Examples

collapse all

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

Load sample data.

load('fisheriris') X = meas; size(X)
ans = 150 4

Convert the matrix to a dataset array.

ds = mat2dataset(X); size(ds)
ans = 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,mat2datasetuses the matrix name and column numbers to create default variable names.

Load sample data.

load('fisheriris') X = meas; size(X)
ans = 150 4

数据集矩阵转换为一个数组,提供一个variable name for each of the four column ofX.

ds = mat2dataset(X,'VarNames',{'SLength',...'SWidth','PLength','PWidth'}); size(ds)
ans = 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

Convert a matrix to a dataset array containing multicolumn variables.

Load sample data.

load('fisheriris') X = meas; size(X)
ans = 150 4

Convert the matrix to a dataset array, combining the sepal measurements (the first two columns) into one variable namedSepalMeas, and the petal measurements (third and fourth columns) into one variable namesPetalMeas.

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 = 150 2

Input Arguments

collapse all

Input matrix to convert to a dataset array, specified as anM-by-Nnumeric matrix. Each column ofXbecomes a variable in the outputM-by-Ndataset array.

Data Types:single|double

Name-Value Pair Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside single quotes (' '). You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Example:'NumCols',[1,1,2,1]specifies that the 3rd and 4th columns of the input matrix should be combined into a single variable.

collapse all

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

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

Observation names for the output dataset array, specified as the comma-separated pair consisting of'ObsNames'and a 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 inds, 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,mat2datasetcombines multiple columns inXinto a single variable inds. The vector you assign toNumColsmust sum tosize(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

collapse all

Output dataset array, returned by default with a variable for each column ofX, and an observation for each row ofX. If you specifyNumCols, then the number of variables indsis equal to the length of the specified vector of column numbers.

Introduced in R2012b

Was this topic helpful?