Loren on the Art of MATLAB

Turn ideas into MATLAB

Note

Loren on the Art of MATLAB has been archived and will not be updated.

Structure Initialization

This post continues in the theme of other recent ones, questions frequently posed to me (and probably others). It has to do with initializing structures. There is a healthy set of posts on the MATLAB newsgroup devoted to this topic. So let's peel things apart today.

Contents

Structures - Mental Model

It first helps to understand how MATLAB treats structures and their fields. First clear the workspace.

clear variables
close all

Let's just start with a scalar structure.

mystruct.FirstName = 'Loren';
mystruct.Height = 150
mystruct = 
    FirstName: 'Loren'
       Height: 150

Each field in the structure mystruct appears to be a separate MATLAB array. The first one, FirstName, is a string of length 5, and the second, height, is a scalar double (in cm, for those who are paying attention to units).

I can add another field and its contents can be the contents of any valid MATLAB variable. Each field is independent in size and datatype.

Array of structs

Suppose I want to extend this array to include other people and measurements. I can grow this array an element at a time.

mystruct(2).FirstName = 'Fred';
mystruct(2)
ans = 
    FirstName: 'Fred'
       Height: []

You can see here that since the field Height does not yet have a value, its value is set to empty ([]).

Don't Grow Arrays

Over the years, we have learned that growing arrays is a poor use of resources in MATLAB and that preallocation is helpful in terms of both not fragmenting memory and not spending time looking for a large enough memory slot. So, if I know I want to have 100 names in my struct, I can initialize the struct to be the right size. I may or may not feel the need to initialize the contents of the struct array however, since each field element is essentially its own MATLAB array.

How to Initialize a struct Array

Here are 2 ways to initialize the struct.

mystruct(100).FirstName = 'George';

With this method, we can see that elements are filled in with empty arrays.

mystruct(17)
ans = 
    FirstName: []
       Height: []

There's another way to initialize the struct and that is fill it with initial values.

If we were building our struct with the 5 sons of George Forman, we might create it like this.

georgeStruct = struct('FirstName','George','Height', ...
    {195 189 190 194 193})
georgeStruct = 
1x5 struct array with fields:
    FirstName
    Height

Looking at the contents of georgeStruct we see that his sons are all named George

{georgeStruct.FirstName}
ans = 
    'George'    'George'    'George'    'George'    'George'

and I made up their heights

[georgeStruct.Height]
ans =
   195   189   190   194   193

To see when and how to use cell arrays in the initialization, read the struct reference page carefully. If you want a field to contain a cell array, you must embed that cell inside another cell array.

Initializing the Contents

How important is it to initialize the contents of the struct. Of course it depends on your specifics, but since each field is its own MATLAB array, there is not necessarily a need to initialize them all up front. The key however is to try to not grow either the struct itself or any of its contents incrementally.

Your Use of Structures

What do you use structures for? Are you able to populate the contents of your struct up front? Or at least pin down the sizes early in your application? To tell me about your usage, please post details here.




Published with MATLAB® 7.5


  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.