next up previous contents
Next: STRING String Arrays Up: Variables and Arrays Previous: Variables and Arrays   Contents

Subsections

STRUCT Structure Array Constructor

Usage

Creates an array of structures from a set of field, value pairs. The syntax is

   y = struct(n1,v1,n2,v2,...)

where ni are the names of the fields in the structure array, and vi are the values. The values v_i must either all be scalars, or be cell-arrays of all the same dimensions. In the latter case, the output structure array will have dimensions dictated by this common size. Scalar entries for the v_i are replicated to fill out their dimensions. An error is raised if the inputs are not properly matched (i.e., are not pairs of field names and values), or if the size of any two non-scalar values cell-arrays are different.

Example

This example creates a 3-element structure array with two fields, foo and bar, where the contents of foo are provided explicitly, and the contents of bar are replicated from a scalar.

--> y = struct('foo',{1,3,4},'bar',{'cheese','cola','beer'},'key',508)
y = 
  <structure array> - size: [1 3]
  Fields
    foo
    bar
    key
--> y(1)
ans = 
  <structure array> - size: [1 1]
    foo: [1]
    bar: cheese
    key: [508]
--> y(2)
ans = 
  <structure array> - size: [1 1]
    foo: [3]
    bar: cola
    key: [508]
--> y(3)
ans = 
  <structure array> - size: [1 1]
    foo: [4]
    bar: beer
    key: [508]



Samit K. Basu 2005-03-16