Structures
For Storing Related but Disparate Information in a Tree-like Format
"You're so beautiful, Like a tree" - Flight of the Conchords. The Most Beautiful Girl in the Room
Overview
Structure arrays have a hierarchal, trunk-and-branch data structure. They are useful for organizing disparate information and variable types into a single variable.
Learning Objectives
After finishing this module, you should be able to:
- Explain why you would use a structure array
- Compare a structure array to a cell and a character array
- Assign a value to a structure array using dot notation
- Access field content in a structure
Relevant MATLAB Documentation
Important Terminology
-
Structure: a data type that groups related data using data containers called fields. Each field can contain data of any type or size.
-
Dot Notation: using the period after a variable name to create or access structure fields
Creating a Structure
A structure array is a hierarchal data structure that groups data into fields. Structure arrays are very useful for organizing complex data, such as the metadata from image files.
The good news about structures is that is very simple and intuitive to make a structure. Simply type a new variable name, append a period (.
) to the end of the name, and then add a field name. This is known as dot notation.
For example, the following syntax creates a new structure called img_info
, with three new fields: "filename", "rows", and "cols".
img_info =
struct with fields:
filename: 'moonshot.jpg'
rows: 25
cols: 50
…Notice that fields can be of any data array class, including character and numeric.
Accessing Field Content
To access the content from a field in a structure, you use dot notation, as follows:
Accessing fields in structures using dot notation | |
---|---|
Pro-Tip: TAB for a list of structure fields
To automatically bring up a list of all of the filenames in a structure, do the following
1. Type the variable name of the structure
2. Type the dot (img_info.
)
3. Then hit the TAB button.
A dialog box containing all field names of the structure should appear. Once the field name dialog appears, select a field name using the UP and DOWN arrows and then ENTER. This technique is VERY useful when there are a lot of field names.
Creating an Array of Structures
Like all MATLAB variables, a structure can also be an array. This is a known as a Nonscalar Structure Array (remember, scalar means having just one element).
Think of it like this: each element in the array contains a structure. For this to work, all the structures in the array must have the same fieldnames, but the data stored in the fieldnames can be different.
You add new elements to a structure array as you would to a numeric array: by using the parentheses to indicate the index of the element.
Nonscalar Structure Array
Adding a new element to our Nonscalar Structure array | |
---|---|
We can be visualize the elements of the nonscalar structure array like this
Accessing Field Content in a Structure Array
So, how do you get the data out? To get the data from the first element in the array, simply index the structure name, as follows:
Accessing content from the rows field in element 1 of the nonscalar structure | |
---|---|
If you do not include the index, then MATLAB returns the values from all the fields sequentially.
Accessing content from the cols field across all elements in nonscalar structure | |
---|---|
…Notice that the output is a comma-separated list, spitting out the contents, one after the other, overwriting ans
each time with the new content. This is similar to what happens when we access multiple cells in a cell array using the curly brackets.
To concatenate the output, you must include a concatenating special character (i.e. [ ]
or { }
):
>>cols = [img_info.cols]
cols =
50 200
This works for scalar content, like numbers. But for nonscalar content, like a character array, you need to use the curly brackets and package the data into a cell array.
Concatenating Character arrays from all structure elements | |
---|---|
…The above syntax extracts the filenames from the filename
field across all elements of img_info and concatenates them into a new 1X2 cell array called filenames
.
Challenge
Show the syntax to sum the values from rows field in img_info
Show the syntax to sum the values from rows field in img_info
:
Without the square brackets, you would get a sum of 25. Why?
What happens when you use the Square Brackets to concatenate the content from the filename fields?
What happens when you use the Square Brackets to concatenate the content from the filename fields?
You get the two filenames crammed into one character array
Advanced Topic: Programmatically Accessing a Structure Field
A somewhat complicated example
Sometimes you want to access the fields in a structure programmatically, i.e. using a variable to indicate the field. To do so, you can use parentheses, as follows:
structure_name.('name_of_field')
Do not confuse this syntax with indexing an array. Notice that in this syntax the period immediate proceeds the parentheses. Inside the parentheses, the field name is entered as a character array or a variable containing a character array. For example:
Creating a dialog to select a field name in a structure
If you would like to access the content from a structure after receiving user input, you could use the following programmatic steps:
First, get the field names from the img_info
using the function fieldnames
. This function returns all the field names in from a structure array as a cell array.
Get Structure Field names | |
---|---|
…Notice the output fld_names
is a cell array.
We can plug this cell array into one of MATLAB's built-in dialog functions, listdlg
. This function creates a dialog in which the user can select one element from the inputed cell array:
Create a list dialog for user input | |
---|---|
The listdlg function creates the following dialog window.
…Notice that the field names from img_info
are listed in the dialog window. Also notice that you select one of the rows.
After you make your selection and you click 'OK', listdlg
returns the index of the rows selected and assigns that value to answer
. If you click "Cancel", listdlg
sets answer
to empty.
We can use the value in answer
to index the input cell array fld_names
Get selected field | |
---|---|
Then we can display the contents of the selected field from the img_info
structure array using the following syntax:
Get Contents from selected field | |
---|---|
In the above example, the values from the row field from both elements of the structure array are displayed. However, if we indicate the index of the element, then we would get the result from the field in that element of the structure. In the following example, we indicate the second element of the structure array and get just one result.
Get contents from just one element in the structure array | |
---|---|
FIN. 🌳