| Programming and Data Types | ![]() |
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
patient(2).ssn = '000-00-0000';
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.
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
struc2= rmfield(array,'field')
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:
patient = rmfield(patient,'name');
| Accessing Data in Structure Arrays | Applying Functions and Operators | ![]() |