# Applying equation to each field in a structure

10 views (last 30 days)
Briana Phillips on 15 Aug 2017
Answered: Teja Muppirala on 16 Aug 2017
I have a structure from an ocean buoy that I am trying to manipulate. The structure has 4 fields air temperature, wind speed, sst, and qa. I want to apply an equation to each field without having to copy past 4 times. I just know there has to be a better way
for i = 1:365
airt(i) = 1/K*sum(buoy.airt((i-1)*K+1 : i*K)) ;
sst(i) = 1/K*sum(buoy.sst((i-1)*K+1 : i*K)) ;
wspd(i) = 1/K*sum(buoy.wspd((i-1)*K+1 : i*K)) ;
qa(i) = 1/K*sum(buoy.qa((i-1)*K+1 : i*K)) ;
end

Stephen Cobeldick on 16 Aug 2017
Edited: Stephen Cobeldick on 16 Aug 2017
Personally I would define a simple anonymous function:
fun = @(ii,val) 1/K*sum(val((ii-1)*K+1:ii*K));
for ii = 1:365
airt(ii) = fun(ii,buoy.airt);
sst(ii) = fun(ii,buoy.sst);
wspd(ii) = fun(ii,buoy.wspd);
qa(ii) = fun(ii,buoy.qa);
end
Remember to preallocate the output arrays before the loop!
You could also try using structfun, but I suspect it would be more complex.

Teja Muppirala on 16 Aug 2017
For 4 variables, copy/pasting seems doable, but if you had a lot more I can see how it could be cumbersome.
This is a solution that uses STRUCTFUN and ARRAYFUN. We generate a new struct "result" that has all the new values for each variable:
%%Make some fake random data
K = 10; % For example, K = 10
buoy = struct('airt',randn(1,365*K), 'sst',randn(1,365*K),'wspd',randn(1,365*K),'qa',randn(1,365*K))
%%Method 1, use a loop. Simple to understand, but hard to scale.
for i = 1:365
airt(i) = 1/K*sum(buoy.airt((i-1)*K+1 : i*K)) ;
sst(i) = 1/K*sum(buoy.sst((i-1)*K+1 : i*K)) ;
wspd(i) = 1/K*sum(buoy.wspd((i-1)*K+1 : i*K)) ;
qa(i) = 1/K*sum(buoy.qa((i-1)*K+1 : i*K)) ;
end
%%Method 2, Using STRUCTFUN and ARRAYFUN.
% Admittedly not very readable... but at least you don't have to copy/paste for every variable
innerFunction = @(X,ii) 1/K*sum(X((ii-1)*K+1 : ii*K)) ;
result = structfun( @(X) arrayfun(@(ii) innerFunction(X, ii),1:365), buoy, 'uniform',false) % <-- Your answer
% Verify that they do indeed give you the same answers.
isequal(airt, result.airt) % TRUE
isequal(sst, result.sst) % TRUE
isequal(wspd, result.wspd) %TRUE
isequal(qa, result.qa) %TRUE