MATLAB Examples

# Create Dataset Array from Heterogeneous Workspace Variables

This example shows how to create a dataset array from heterogeneous variables existing in the MATLAB® workspace.

## Contents

```load carsmall ```

## Create a dataset array.

Create a dataset array from a subset of the workspace variables.

```ds = dataset(Origin,Acceleration,Cylinders,MPG); ds.Properties.VarNames(:) ```
```ans = 4x1 cell array {'Origin' } {'Acceleration'} {'Cylinders' } {'MPG' } ```

When creating the dataset array, you do not need to enter variable names. dataset automatically uses the name of each workspace variable.

Notice that the dataset array, ds, contains a collection of variables with heterogeneous data types. Origin is a character array, and the other variables are numeric.

## Examine a dataset array.

Display the first five observations in the dataset array.

```ds(1:5,:) ```
```ans = Origin Acceleration Cylinders MPG USA 12 8 18 USA 11.5 8 15 USA 11 8 18 USA 12 8 16 USA 10.5 8 17 ```

## Apply a function to a dataset array.

Use datasetfun to return the data type of each variable in ds.

```varclass = datasetfun(@class,ds,'UniformOutput',false); varclass(:) ```
```ans = 4x1 cell array {'char' } {'double'} {'double'} {'double'} ```

```ds.Cylinders = nominal(ds.Cylinders,{'four','six','eight'}); ds(1:15,{'Origin','Cylinders'}) ```
```ans = Origin Cylinders USA eight USA eight USA eight USA eight USA eight USA eight USA eight USA eight USA eight USA eight France four USA eight USA eight USA eight USA eight ```