%% Apply Function to Field of Structure Array
% Create a nonscalar structure array. Each structure has a field that contains a vector of random numbers. The vectors have different sizes.

S(1).f1 = rand(1,5);
S(2).f1 = rand(1,10);
S(3).f1 = rand(1,15)

%
% Calculate the mean for each field in S by using the arrayfun function. You cannot use structfun for this calculation because the input argument to structfun must be a scalar structure.

A = arrayfun(@(x) mean(x.f1),S)

% Copyright 2012 The MathWorks, Inc.