Documentation Center

  • Trial Software
  • Product Updates

subsref

Class: dataset

Subscripted reference for dataset array

The dataset data type might be removed in a future release. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information.

Syntax

B = subsref(A,S)

Description

B = subsref(A,S) is called for the syntax A(i,j), A{i,j}, or A.var when A is a dataset array. S is a structure array with the fields:

typeString containing '()', '{}', or '.' specifying the subscript type.
subsCell array or string containing the actual subscripts.

B = A(i,j) returns a dataset array that contains a subset of the observations and variables in the dataset array A. i and j are one of the following types:

  • positive integers

  • vectors of positive integers

  • observation/variable names

  • cell arrays containing one or more observation/variable names

  • logical vectors

B contains the same property values as A, subsetted for observations or variables where appropriate.

B = A{i,j} returns an element of a dataset variable. i and j are positive integers, or logical vectors. Cell indexing cannot return multiple dataset elements, that is, the subscripts i and j must each refer to only a single observation or variable. A{i,j} may also be followed by further subscripting as supported by the variable.

For dataset variables that are cell arrays, expressions such as A{1,'CellVar'} return the contents of the referenced dataset element in the same way that {}-indexing on an ordinary cell array does. If the dataset variable is a single column of cells, the contents of a single cell is returned. If the dataset variable has multiple columns or is n-D, multiple outputs containing the contents of multiple cells are returned.

For dataset variables that are n-D arrays, i.e., each observation is a matrix or an array, expressions such as A{1,'ArrayVar'} return A.ArrayVar(1,:,...) with the leading singleton dimension squeezed out.

B = A.var or A.(varname) returns a dataset variable. var is a variable name literal, or varname is a character variable containing a variable name. A.var or A.(varname) may also be followed by further subscripting as supported by the variable. In particular, A.var(obsnames,...) and A.var{obsnames,...} (when supported by var) provide subscripting into a dataset variable using observation names.

P = A.Properties.propertyname returns a dataset property. propertyname is one of the following:

  • 'ObsNames'

  • 'VarNames'

  • 'Description'

  • 'Units'

  • 'DimNames'

  • 'UserData'

  • 'VarDescription'

A.properties.propertyname may also be followed by further subscripting as supported by the property.

Limitations

Subscripting expressions such as A.CellVar{1:2}, A.StructVar(1:2).field, or A.Properties.ObsNames{1:2} are valid, but result in subsref returning multiple outputs in the form of a comma-separated list. If you explicitly assign to output arguments on the left-hand side of an assignment, for example, [cellval1,cellval2] = A.CellVar{1:2}, those variables will receive the corresponding values. However, if there are no output arguments, only the first output in the comma-separated list is returned.

Similarly, if a dataset variable is a cell array with multiple columns or is an n-D cell array, then subscripting expressions such as A{1,'CellVar'} result in subsref returning the contents of multiple cells. You should explicitly assign to output arguments on the left-hand side of an assignment, for example, [cellval1,cellval2] = A{1,'CellVar'}.

See Also

| |

Was this topic helpful?