Skip to main content

6.5) The Structure Class


Structures are arrays with named fields that can contain data of varying types and sizes. They allow mixed data to be stored in single objects, and differ from other arrays in several ways:

  • They use referencing by name rather than number.
  • A dot is used to append names to the structure.
  • A special "struct" function can be used to add new entries.

Both cell arrays and structures are useful containers of data, and for storing metadata alongside data.

For example, it's useful to store model data and metadata within a structure:

[matlab]
>> model(1).name='HadCM3';
>> model(1).experiment='control';
>> model(1).lat=73;
>> model(1).lon=96;
>> model(1).data=load('HadCM3.mat');
>> model(2).name='HadCM3L';
>> model(2).experiment='control';
>> model(2).lat=73;
>> model(2).lon=96;
>> model(2).data=load('HadCM3L.mat');
[/matlab]

Use of the "struct" function to add new entries:

[matlab]
>> model(end+1)=struct('name,'CCSM3.0','experiment','control','grid',[64 128],'data',load(CCSM.mat));
>>
[/matlab]