Variables of class struct in MATLAB


[Back to main page] [Back to language features]

A variable of class struct (which I'll just refer to as a "struct" or "struct variable") is a very handy thing to have. While you can get along fine in MATLAB without struct variables, these beasties are quite useful for more complicated and involved programming projects.

What is a struct? In my simple view of programming, a struct is a variable with personality. As such, structs can be used to represent things that are more complicated than can be expressed by a simple number or set of numbers.

For example, chemical engineers often work in a world where chemical compounds are the fundamental items for consideration and computation. A single number is not enough to express the idea of, say, the compound methane (CH4). Rather, there are many numbers and other bits of information that are needed to express the idea of "methane", items such as

... and so on. The important point of this example is that chemical computations, while numerically based, are usually considered at a higher level of complexity than is expressible by an array of numbers.

A struct is a type of variable (i.e., a class) that allows you to express this higher level of complexity while retaining the simplicity of a single name. For example, you can create a struct variable called methane to hold the information just listed via the function struct as

   >> methane = struct('MW',16,'Cp',109,'Hcomb',85000,'formula','CH4')

in one single step or via the "dot" notation

   >> methane.MW = 16
   >> methane.Cp = 109
   >> methane.Hcomb = 85000
   >> methane.formula = 'CH4'

in several steps. The net result in either case displays as

   methane = 
         MW: 16
         Cp: 109
      Hcomb: 85000
    formula: 'CH4'

Note that a struct has a name (methane in this case) and a set of fields, each of which contains a valid MATLAB variable (a numeric array, a charater array, a cell array or even another structure). A struct allows you to refer to all the information contained in the fields by a single name.

The output of whos confirms the nature of the variable:

   >> whos
   Name          Size         Bytes  Class

   methane       1x1            526  struct array

The Bytes column tells you that methane is more than the standard 8-byte MATLAB 1x1 variable.

Structs have a much in common with cell arrays and indeed MATLAB provides functions that connect these data types (see the help information on struct2cell and cell2struct).

As with cell arrays, care must be exercised in the use of structs. For example, it does not much sense to compute the square root of methane and MATLAB will tell you so;

   >> sqrt(methane)
   ??? Function 'sqrt' not defined for variables of class 'struct'.

However, it might make sense to compute the square root of the molecular weight of methane. To do this, use the dot-notation to extract the field that contains the molecular weight

   >> sqrt(methane.MW)
   ans =
        4

The dot-notation is effectively an indexing method for the equivalent cell array.

Struct are clearly important for developing databases and more complicated "objects" in MATLAB and if you do more complex programming, the efficiencies of using structs will become valuable to you. More information is available in the MATLAB documentation.


[Back to main page] [Back to language features]
Comments? Contact Jim Maneval at maneval@bucknell.edu