Organizing Data in Structure Arrays
The key to organizing structure arrays is to decide how you want to access subsets of the information. This, in turn, determines how you build the array that holds the structures, and how you break up the structure fields.
For example, consider a 128-by-128 RGB image stored in three separate arrays;
There are at least two ways you can organize such data into a structure array.
In the plane organization, shown to the left in the figure above, each field of the structure is an entire plane of the image. You can create this structure using
This approach allows you to easily extract entire image planes for display, filtering, or other tasks that work on the entire image at once. To access the entire red plane, for example, use
Plane organization has the additional advantage of being extensible to multiple images in this case. If you have a number of images, you can store them as
A(3), and so on, each containing an entire image.
The disadvantage of plane organization is evident when you need to access subsets of the planes. To access a subimage, for example, you need to access each field separately:
The element-by-element organization, shown to the right in the figure above, has the advantage of allowing easy access to subsets of data. To set up the data in this organization, use
With element-by-element organization, you can access a subset of data with a single statement:
To access an entire plane of the image using the element-by-element method, however, requires a loop:
Element-by-element organization is not the best structure array choice for most image processing applications; however, it can be the best for other applications wherein you will routinely need to access corresponding subsets of structure fields. The example in the following section demonstrates this type of application.
Example -- A Simple Database
Consider organizing a simple database.
Each of the possible organizations has advantages depending on how you want to access the data:
client.m, which displays the name and address of a given client on screen.
addressfield to accommodate longer strings.
Typically, your data does not dictate the organization scheme you choose. Rather, you must consider how you want to access and operate on the data.
|Writing Functions to Operate on Structures||Nesting Structures|
© 1994-2005 The MathWorks, Inc.