Programming Previous page   Next Page

Finding the Size of Structure Arrays

Use the size function to obtain the size of a structure array, or of any structure field. Given a structure array name as an argument, size returns a vector of array dimensions. Given an argument in the form array(n).field, the size function returns a vector containing the size of the field contents.

For example, for the 1-by-3 structure array patient, size(patient) returns the vector [1 3]. The statement size(patient(1,2).name) returns the length of the name string for element (1,2) of patient.

Adding Fields to Structures

You can add a field to every structure in an array by adding the field to a single structure. For example, to add a social security number field to the patient array, use an assignment like

Now patient(2).ssn has the assigned value. Every other structure in the array also has the ssn field, but these fields contain the empty matrix until you explicitly assign a value to them.

To add new fields to a structure, specifying the names for these fields at run-time, see the section on Using Dynamic Field Names.

Deleting Fields from Structures

You can remove a given field from every structure within a structure array using the rmfield function. Its most basic form is

where array is a structure array and 'field' is the name of a field to remove from it. To remove the name field from the patient array, for example, enter


Previous page  Using Dynamic Field Names Applying Functions and Operators Next page

© 1994-2005 The MathWorks, Inc.