When you have data that you want to organize by name, you can use structures to store it. Structures store data in containers called fields, which you can then access by the names you specify. Use dot notation to create, assign, and access data in structure fields. If the value stored in a field is an array, then you can use array indexing to access elements of the array. When you store multiple structures as a structure array, you can use array indexing and dot notation to access individual structures and their fields.
Create Scalar Structure
First, create a structure named
patient that has fields storing data about a patient. The diagram shows how the structure stores data. A structure like
patient is also referred to as a scalar structure because the variable stores one structure.
Use dot notation to add the fields
test, assigning data to each field. In this example, the syntax
patient.name creates both the structure and its first field. The commands that follow add more fields.
patient.name = 'John Doe'; patient.billing = 127; patient.test = [79 75 73; 180 178 177.5; 220 210 205]
patient = struct with fields: name: 'John Doe' billing: 127 test: [3x3 double]
Access Values in Fields
After you create a field, you can keep using dot notation to access and change the value it stores.
For example, change the value of the
patient.billing = 512.00
patient = struct with fields: name: 'John Doe' billing: 512 test: [3x3 double]
With dot notation, you also can access the value of any field. For example, make a bar chart of the values in
patient.test. Add a title with the text in
patient.name. If a field stores an array, then this syntax returns the whole array.
bar(patient.test) title("Test Results for " + patient.name)
To access part of an array stored in a field, add indices that are appropriate for the size and type of the array. For example, create a bar chart of the data in one column of
Index into Nonscalar Structure Array
Structure arrays can be nonscalar. You can create a structure array having any size, as long as each structure in the array has the same fields.
For example, add a second structure to
patients having data about a second patient. Also, assign the original value of
127 to the
billing field of the first structure. Since the array now has two structures, you must access the first structure by indexing, as in
patient(1).billing = 127.
patient(2).name = 'Ann Lane'; patient(2).billing = 28.50; patient(2).test = [68 70 68; 118 118 119; 172 170 169]; patient(1).billing = 127
patient=1×2 struct array with fields: name billing test
As a result,
patient is a 1-by-2 structure array with contents shown in the diagram.
Each patient record in the array is a structure of class
struct. An array of structures is sometimes referred to as a struct array. However, the terms struct array and structure array mean the same thing. Like other MATLAB® arrays, a structure array can have any dimensions.
A structure array has the following properties:
All structures in the array have the same number of fields.
All structures have the same field names.
Fields of the same name in different structures can contain different types or sizes of data.
If you add a new structure to the array without specifying all of its fields, then the unspecified fields contain empty arrays.
patient(3).name = 'New Name'; patient(3)
ans = struct with fields: name: 'New Name' billing:  test: 
To index into a structure array, use array indexing. For example,
patient(2) returns the second structure.
ans = struct with fields: name: 'Ann Lane' billing: 28.5000 test: [3x3 double]
To access a field, use array indexing and dot notation. For example, return the value of the
billing field for the second patient.
ans = 28.5000
You also can index into an array stored by a field. Create a bar chart displaying only the first two columns of
You can index into part of a field only when you refer to a single element of a
structure array. MATLAB® does not support statements such as
patient(1:2).test(1:2,2:3), which attempt to index into a field for
multiple elements of the structure array. Instead, use the