Main Content

substruct

Create indexing structure argument

Description

S = substruct(type,subs) creates a structure with the information required by the subsref and subsasgn functions. The type argument indicates whether the indexing expression is parentheses, dot, or brace. The corresponding subs argument is a field name for dot indexing or a cell array containing index vectors for parentheses or brace indexing.

Note

Performing indexing by calling the subsref or subsasgn function explicitly is always slower than the equivalent indexing statement.

example

S = substruct(type1,subs1,type2,subs2,...) creates a structure array for a compound indexing expression, with each type and subs pair representing one indexing expression.

example

Examples

collapse all

Create a 2-by-2 array and display the element at (1,2) using the standard indexing syntax.

A = magic(2);
A(1,2)
ans =

     3

Use the substruct function to create a structure that describes the indexing operation.

S = substruct("()",{1,2})
S = 

  struct with fields:

    type: "()"
    subs: {[1]  [2]}

Call subsref with the result from substruct to perform the operation equivalent to A(1,2).

subsref(A,S)
ans =

     3

Create a 1-by-2 structure array x with fields name and numbers. Display the data numbers(3:4) of the entry at x(2).

x.name = "Clara";
x.numbers = [1 3 5 7];
x(2).name = "Jorge";
x(2).numbers = [2 4 6 8];
x(2).numbers(3:4)
ans =

     6     8

x(2).numbers(3:4) has three indexing expressions: a parentheses, a dot, and a second parentheses expression. Use the substruct function to create a structure array that describes the indexing expression.

 S = substruct("()",{2},".","numbers","()",{3:4});

Call subsref using the result from substruct to perform the operation equivalent to x(2).numbers(3:4).

subsref(x,S)
ans =

     6     8

Input Arguments

collapse all

Type of indexing expression, specified as "()", ".", or "{}" to indicate parentheses, dot, or brace indexing, respectively. The value can be a character vector or string scalar.

Subscript values, specified as a character vector or string scalar for dot indexing or a cell array of index vectors for parentheses or brace indexing.

Output Arguments

collapse all

Indexing argument structure, also referred to as the substruct, specified as a structure array. Each structure has two fields:

  • type — Type of indexing expression, specified as "()", ".", or "{}" to indicate parentheses, dot, or brace indexing, respectively. The value can be a character vector or string scalar.

  • subs — Subscript values, specified as a character vector or string for dot indexing or a cell array of index vectors for parentheses or brace indexing.

S is a scalar structure for single indexing expressions, like A{1}, or a structure array for compound indexing expressions. For example, A{1}.field(3:5) has three levels of indexing. For this expression, S is a 1-by-3 structure array:

S(1)

type: '{}'

subs: {[1]}

S(2)

type: '.'

subs: 'field'

S(3)

type: '()'

subs: {[3 4 5]}

Tips

Extended Capabilities

expand all

Version History

Introduced before R2006a