You can write a custom function add_val2(x, y)that adds yto the field x.val2and calls cellfun()with @add_val2instead @plus.
First create a function add_val2.m:
function x = add_val2(x, y)
x.val2 = x.val2 + y;
end
Then the challenge cellfun()is simple as
out = cellfun(@add_val2, cellArray, {50, 50}, 'UniformOutput', false);
that leads to
>> out{1}
ans =
struct with fields:
val1: 10
val2: 70
>> out{2}
ans =
struct with fields:
val1: 1000
val2: 2050
source
share