%% Creating a structure example
%
%

%% 
% Suppose we have profile data from a cruise. We can create a structure
% cruisedata with a field for the cruise name, the station numbers and
% their corresponding latitudes and longitudes.
%
cruisedata.cruisename = 'Shelf Jan 2008';
cruisedata.stations = [1:10];
cruisedata.lat = [53.4:0.1:54.3];
cruisedata.lon = [22:0.1:22.9];

%%
% Note that the dot separates the structure name from the field name.
%
% To see the whole structure, we just enter its name:
cruisedata

%%
% We can access individual elements like we do in arrays. For example, 
% to access the latitude and longitude of the 2nd station:
cruisedata.lat(2)
cruisedata.lon(2)

%% 
% We could add a second cruise to the structure.
cruisedata(2).cruisename = 'Shelf May 2008';
cruisedata(2).stations = [1:5];
cruisedata(2).lat = [53.4:0.2:54.3];
cruisedata(2).lon = [22:0.1:22.4];

%%
% Now entering the structure name just gives us a summary of the structure:
cruisedata

%%
% The first cruise is now accessed as:
cruisedata(1)


%%
% We can access a subset of stations from the 2nd cruise as:
cruisedata(2).stations(1:3)

%%
% We can access all cruise names as:
cruisedata.cruisename


%%
% Note that the field sizes don't have to conform across elements of the
% structure, e.g. cruisedata(1) has 10 stations and cruisedata(2) has 5.
%
% A structure can contain any kind of data, even another structure. For
% example, we could create a structure with the profile data for each 
% cruise.
%
data(1).temp = 18*ones(1,10);
data(1).salt = 36*ones(1,10);
data(1).no3  = 10*ones(1,10);
data(1).depth = [1:10];
for p=2:5
  data(p).temp = 18*ones(1,5);
  data(p).salt = 36*ones(1,5);
  data(p).no3  = 10*ones(1,5);
  data(p).depth = [1:5];
end

%%
% Now we can feed this profile data into a data field in cruisedata(2).
cruisedata(2).data = data;

%%
% We could create a similar data structure and assign it to the data field
% in cruisedata(1).
%
data(1).temp = 18*ones(1,13);
data(1).salt = 36*ones(1,13);
data(1).no3  = 10*ones(1,13);
data(1).depth = [1:13];
for p=2:10
  data(p).temp = 18*ones(1,5);
  data(p).salt = 36*ones(1,5);
  data(p).no3  = 10*ones(1,5);
  data(p).depth = [1:5];
end

cruisedata(1).data = data;

%%
% You can obtain more information about structures with the 
% help struct command.

