STRUCTURES

overview

Structures have a hierarchal data structure
Structures have a hierarchal data structure

Structure arrays have a hierarchal branch and truck like structure. They are useful for collecting disparate information and variable types into a single variable.

This module is broken down into the following sections:

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

Create a Structure

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

Important MATLAB Functions

  • rmfield – Remove fields from structure
  • fieldnames – get the Field names of a structure

Creating a Structure

TOP | Creating a Structure | Creating a Struct Array | Accessing Field Content in a Structure array | Programmatically Accessing a Structure Field

A structure is a hierarchal data structure that groups data into fields. Structures are very useful for organizing complex data, such as the metadata from image files.

It is very simple and intuitive to make a structure. Simply append a period (.) to a variable name and then add a field name. This is known as dot notation.

Let’s try it now. Enter the following three lines in the command window:

img_info.filename = 'moonshot.jpg'
img_info.rows = 25
img_info.cols = 50

The result is a new structure named img_info with three fields: filename, rows, cols. Notice that fields can be of any data array class, including character and numeric.

img_info = 

    filename: 'moonshot.jpg'
        rows: 25
        cols: 50

whos img_info
Name       Size            Bytes  Class     Attributes

  image      1x1               568  struct              


Accessing Field Content

To access the content from a field in a structure, you use dot notation, as follows:

img_info.rows

ans =

    25

TIP: To automatically bring up a list of all of the filenames in a structure, type the variable name of the structure followed by a dot (img_info.) and then hit the TAB button. A box containing all the filenames of the structure should appear:

Click on the UP and DOWN arrows to select the field of interest. This is VERY useful when there are a lot of fieldnames


Creating a Struct Array

TOP | Creating a Structure | Accessing Field Content | Creating a Struct Array | Programmatically Accessing a Structure Field

Like all MATLAB variables, a structure is also an array. This type of variable is called a struct array. Struct arrays have the following properties:

  • All structs in the array have the same number of fields.
  • All structs have the same field names.
  • Fields of the same name in different structs can contain different types or sizes of data

You can add new elements to a structure array as you would to a cell or a numeric array: by indicating the index of the element. For example:

img_info(2).filename = 'apollo13.jpg'
img_info(2).rows = 100
img_info(2).cols = 200

Now you have a 1X2 struct array:

img_info = 

1x2 struct array with fields:

    filename
    rows
    cols

To get all the filenames from this structure array, use the following syntax:

>>filenames = {img_info.filename}

filenames = 

    'moonshot.jpg'    'apollo13.jpg'

The above syntax returns the filenames for both element entries in img_info and then concatenates these strings into a new 1X2 cell array called filenames.

Try it now: What happens when you don’t include the curly brackets?


Accessing Field Content in a Structure array

TOP | Creating a Structure | Creating a Struct Array | Accessing Field Content in a Structure array | Programmatically Accessing a Structure Field

To access the contents in a structure array, simply enter the structure name, the index, and the field name as such:

>>img_info(1).rows
ans = 
      25

If you do not include the index, then MATLAB returns the values from all the fields sequentially.

img.cols

img_info.cols

ans =

    50

ans =

   200

Notice that ans is overwritten each time by the contents in the currently indexed field. This is why you must include a concatenating special character (i.e. [ ] or { }) if you would like to capture the output to a new variable, as we did above with the filenames example.

>>cols = [img_info.cols]
cols =

    50   200


Programmatically Accessing a Structure Field

TOP | Creating a Structure | Creating a Struct Array | Accessing Field Content in a Structure array | Programmatically Accessing a Structure Field

Sometimes you want to access all the fields in a structure. Or you want to have a field accessed programmatically. To access a field programmatically, you use the following syntax:

structure_name.('name_of_field')

Do not confuse this syntax with indexing an array. Notice that in this syntax, you entered the field name as a character array inside the parentheses. For example:

img_info.('filename')

For example, if you would like to access the content from a structure structure after receiving user input, you could use the following programmatic steps.

First, let’s 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.

>>fld_names = fieldnames(img_info)

fld_names = 

    'filename'
    'rows'
    'cols'

Notice that 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:

>> answer = listdlg('ListString',fld_names)

Note that listdlg returns the index of the element selected. We can use that value to index the input cell array fld_names

>>selected_field = fld_names{answer}

selected_field =

rows

Then we can display the contents of the selected field from the img_info structure array using the following syntax:

>>img_info.(selected_field)

ans =

    25


ans =

   100

In the above example, the values from the row field from both elements of the struct 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 struct array and get just one result.

img_info(2).(selected_field)

ans =

    100

FIN.