| Using the C Math Library | ![]() |
Overview
A MATLAB structure is very much like a structure in C; it is a variable that contains other variables. Each of the contained variables is called a field of the structure, and each field has a unique name. For example, imagine you were building a database of images. You might want to create a structure with three fields: the image data, a description of the image, and the date the image was created. The following MATLAB code creates this structure:
images.image = image1; images.description = 'Trees at Sunset'; images.date.year = 1998; images.date.month = 12; images.date.day = 17;
The structure images contains three fields: image, description and date. The date field is itself a structure, and contains three additional fields: year, month and day. Notice that structures can contain different types of data. images contains a matrix (the image), a string (the description), and another structure (the date).
Like standard arrays, structures are inherently array oriented. A single structure is a 1-by-1 structure array, just as the value 5 is a 1-by-1 numeric array. You can build structure arrays with any valid size or shape, including multidimensional structure arrays. Assume you wanted to arrange the images from the previous example in a series of "pages," where each page is three images wide (three columns) and four images tall (four rows). The images might be arranged this way in a photo album, or for publication in a journal. The following code demonstrates how you use standard MATLAB indexing to create and access the elements of a 3-by-4-by-n structure array:
images(3,4,2).image = image24; images(3,4,2).description = 'Greater Bird of Paradise'; images(3,4,2).date.year = 1993; images(3,4,2).date.month = 7; images(3,4,2).day = 15;
For simplicity, the examples in the book focus on two-dimensional structure arrays, but they'd work just as well with structure arrays of any dimension.
Tips for Working with Structure Arrays
mlfIndexRef(), mlfIndexAssign(), mlfIndexDelete().x.description, for example, you'd get a 24-by-1 array of strings containing all the image descriptions in the structure array. | Structure Array Indexing | Accessing a Field | ![]() |