What does the MATLAB error “scalar structure required for this assignment” refer to in this statement?

You are creating a 1x5 struct array. To quote the struct documentation:

If value is a cell array, then s is a structure array with the same dimensions as value. Each element of s contains the corresponding element of value.

Since the second argument in the expression struct( 'Ps', cell( 1, length(Ps) ) ) is a 1x5 cell, the output struct will be a 1x5 struct array, and the proper assignment in the for-loop will be

space_averaged_data(p).Ps = mean( Ps{p}, 2 );

To get the behavior you desire, wrap the second argument in {} to make it a 1x1 cell array:

space_averaged_data = struct( 'Ps', {cell( 1, length(Ps) )} );

and the for-loop should work as expected.

Leave a Comment