Structure variables

Structure variables are variables that have many fields of information. They are a very powerful and compact way to organize information in MATLAB.

For instance, an easy way to think about structures is in the context of storing the values of metal properties. Each metal has characteristic properties such as density, specific heat constant, melting point, Young's modulus, etc. These properties are listed in Table 6.2. For each of these properties an associated field in the structure variable can be created.


Table 6.2: Metal properties
  Metal Density Specific heat Melting point Young's modulus
1. Steel 7.85 g/cm$ ^3$ 486 J/kgK 371 K 209 GPa
2. Gold 19.32 g/cm$ ^3$ 130 J/kgK 1357 K 74,5 GPa
... ... ... ... ... ...


We can create this table of metal properties in MATLAB by typing the following at the command line:

>> metals(1).name='Steel';
>> metals(1).density=7.85;
>> metals(1).specific_heat=486;
>> metals(1).melting_point=371;
>> metals(1).youngs_modulus=209;

>> metals(2).name='Gold';
>> metals(2).density=19.32;
>> metals(2).specific_heat=130;
>> metals(2).melting_point=1357;
>> metals(2).youngs_modulus=74.5;

In the above example, fields were created by using the dot `.' after the main variable name. To see the field names of the structure variable, just type the variable name, or alternatively, use the fieldnames function, e.g.

>> fieldnames(metals)
To access the value of only one field, type the variable name, index, and field name, e.g.
>> metals(1).name
To see the actual content of all fields for one index of the structure (i.e., all properties of one metal), type the variable name with the appropriate index, e.g.
>> metals(1)



Previous      Next      Up      Contents


Esteur 2010-03-22