Cody

# Problem 43685. Apply Function to Each Field of a Structure Array: Part 1

Solution 1062222

Submitted on 27 Nov 2016 by Binbin Qi
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
s.f1 = 'Sunday'; s.f2 = 'Monday'; s.f3 = 'Tuesday'; s.f4 = 'Wednesday'; s.f5 = 'Thursday'; s.f6 = 'Friday'; s.f7 = 'Saturday'; f = @numel; c = structfun(@numel, s); assert(isequal(structfun2(f,s),c))

ans = cell [7×1 double] ans = 6 6 7 9 8 6 8

2   Pass
s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]}); f = @(x)min(x(:)); c = {[1;1], [3;2]; [1;3], [5;2]}; assert(isequal(structfun2(f,s),c))

ans = 2×2 cell array [2×1 double] [2×1 double] [2×1 double] [2×1 double]

3   Pass
s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]}); f = @(x)max(x(:)); c = {[1;2], [4;2]; [1;5], [6;5]}; assert(isequal(structfun2(f,s),c))

ans = 2×2 cell array [2×1 double] [2×1 double] [2×1 double] [2×1 double]

4   Pass
s = struct('f1',{0, [3 4]; 1, [5 7 6]},'f2',{[0 0], 2; [2 4 5], [0 5]}); f = @nnz; c = {[0;0], [2;1]; [1;3], [3;1]}; assert(isequal(structfun2(f,s),c))

ans = 2×2 cell array [2×1 double] [2×1 double] [2×1 double] [2×1 double]

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!