Ways to Organize Data in Structure Arrays
There are at least two ways you can organize data in a structure array: plane organization and element-by-element organization. The method that best fits your data depends on how you plan to access the data, and, for very large data sets, whether you have system memory constraints.
Plane organization allows easier access to all values within a field. Element-by-element organization allows easier access to all information related to a single element or record. The following sections include an example of each type of organization:
When you create a structure array, MATLAB® stores information about each element and field in the array header. As a result, structures with more elements and fields require more memory than simpler structures that contain the same data.
Consider an RGB image with three arrays corresponding to color intensity values.
If you have arrays
BLUE in your workspace, then these commands create a scalar structure
img that uses plane organization:
img.red = RED; img.green = GREEN; img.blue = BLUE;
Plane organization allows you to easily extract entire image planes for display,
filtering, or other processing. For example, multiply the red intensity values by
adjustedRed = .9 * img.red;
If you have multiple images, you can add them to the
so that each element
img(1),...,img(n) contains an entire image. For an
example that adds elements to a structure, see the following section.
Consider a database with patient information. Each record contains data for the patient’s name, test results, and billing amount.
These statements create an element in a structure array named
patient(1).name = 'John Doe'; patient(1).billing = 127.00; patient(1).test = [79, 75, 73; 180, 178, 177.5; 220, 210, 205];
Additional patients correspond to new elements in the structure. For example, add an element for a second patient:
patient(2).name = 'Ann Lane'; patient(2).billing = 28.50; patient(2).test = [68, 70, 68; 118, 118, 119; 172, 170, 169];
Element-by-element organization supports simple indexing to access data for a
particular patient. For example, find the average of the first patient’s test results,
calculating by rows (dimension
2) rather than by columns:
aveResultsDoe = mean(patient(1).test,2)
This code returns
aveResultsDoe = 75.6667 178.5000 212.0000