Loren on the Art of MATLAB

Turn ideas into MATLAB

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.


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';
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.

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:

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

ans = 
    'George'    'George'    'George'    'George'    'George'

and I made up their heights

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
  • send email


댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.