%% Fields of Elements of Structure Array
% Assign values to fields of elements of a structure array.
% First, create a structure array. As in all structure arrays, each element is a structure with the same fields.

S.x = linspace(0,2*pi);
S.y = sin(S.x);
S(2).x = S.x;
S(2).y = cos(S(2).x)

%
% You also can assign values using setfield. If a field does not exist, setfield creates it. Create a field named title.

S = setfield(S,{1},'title','y = sin(x)')

%
% The setfield function assigns a value to a field of an individual element, but the output argument is the entire structure array.
% Display the first element of S.

S(1)

%
% As an alternative, index into the structure array, and then use dot notation to assign a value to a field of an element.

S(2).title = 'y = cos(x)';
S(2)

% Copyright 2018 The MathWorks, Inc.