The |structfun| function only works on scalar struct arrays, iterating over the fields of that scalar struct. While you _could_ use |arrayfun|, it may be easier to understand (and perform just as well) if you use a |for| loop. I know people have said "Don't use |for| loops in MATLAB, they're slow." While that may have been true in the past (and you can still write a |for| loop so it is slow in current MATLAB) the performance of |for| loops have increased significantly with the improvements we have made to MATLAB over the years.
Besides a |for| loop over 8 elements of a struct array calling |size| is highly unlikely, in my experience, to be the bottleneck in your code. Answer from Steven Lord on mathworks.com
MathWorks
mathworks.com › matlab › language fundamentals › data types › structures
structfun - Apply function to each field of scalar structure - MATLAB
A = structfun(func,S) applies the function func to each field of scalar structure S, one field at a time. structfun then concatenates the outputs from func into the column vector A. The input argument func is a function handle to a function that takes one input argument and returns a scalar.
MathWorks
mathworks.com › matlabcentral › answers › 811680-understanding-the-structfun-or-cellfun-commands
Understanding the structfun() or cellfun() commands - MATLAB Answers - MATLAB Central
April 23, 2021 - As the error message and doc says, structfun applies a function to each field in a scalar struct; you have a struct array -- not what you want. And, for the specific desire you don't need either function nor a loop construct, either -- use MATLAB vectorized notation--
MathWorks
mathworks.com › matlabcentral › answers › 328180-how-to-exclude-some-fields-while-using-the-function-structfun
How to Exclude Some Fields While Using the Function structfun? - MATLAB Answers - MATLAB Central
March 5, 2017 - You can't do it with structfun. ... c = struct2cell(A.B); %convert to cell array. In this particular case, a matrix would work even better · %you can then filter rows (fields) of the cell array any way you want,e .g. ... Sign in to comment. ... https://www.mathworks.com/matlabcentral/answers/328180-how-to-exclude-some-fields-while-using-the-function-structfun#answer_782464
Stack Overflow
stackoverflow.com › questions › 74166018 › matlab-structfun
r - Matlab Structfun - Stack Overflow
x = real(x) #impose non-imaginary # we actually want all the d_ij to be N*N, no N^2, so reshape: z_d = structfun(@(x) reshape(x,z.N,z.N), z_d, 'Uniform',0); #this one · I would say sapply() but I did not succeed yet. For more information, I am trying to translate Matlab code of a solveExactHat.m file from Balboni et al.
MATLAB Central
mathworks.com › matlabcentral › cody › groups › 39795 › problems › 43685
Apply Function to Each Field of a Structure Array: Part 1 - MATLAB Cody - MATLAB Central
That is, structfun2(f,s) = structfun(f,s) holds for any scalar structure s. The assumption required is that f is a function that returns a scalar, regardless of the input. ... s = struct('f1',{1, [3 4]; 1, [5 6]},'f2',{[1 2], 2; [3 4 5], [2 5]}); f = @numel; c = {[1;2], [2;1]; [1;3], [2;2]}; ...
MathWorks
mathworks.com › matlabcentral › answers › 524757-apply-function-to-all-fields-in-a-structure-array
apply function to all fields in a structure array - MATLAB Answers - MATLAB Central
May 11, 2020 - https://www.mathworks.com/matlabcentral/answers/524757-apply-function-to-all-fields-in-a-structure-array#answer_431881 · Cancel Copy to Clipboard · Why the nested structure? That's where the problem arises---convert to a struct array, S as · >> S=F1 · S = 1×2 struct array with fields: f1 · f2 · K>> arrayfun(@(s) structfun(@max,s),S,'UniformOutput',false) ans = 1×2 cell array ·
Mit
lost-contact.mit.edu › afs › inf.ed.ac.uk › group › teaching › matlab-help › R2016b › matlab › ref › structfun.html
structfun
[A1,...,An] = structfun(func,S) applies the function specified by function handle func to each field of scalar structure S.
MathWorks
mathworks.com › matlabcentral › answers › 2062167-accessing-all-fields-within-a-struct-level-at-once
Accessing all fields within a struct level at once - MATLAB Answers - MATLAB Central
December 19, 2023 - https://www.mathworks.com/matlabcentral/answers/2062167-accessing-all-fields-within-a-struct-level-at-once#answer_1374222 · Cancel Copy to Clipboard · Ran in: "Is this possible to achieve without implementing a loop?" Not really, but you can hide the loop using STRUCTFUN: A.one.include = 1; A.two.include = 2; A.three.dontinclude = 3; F = @(s)isfield(s,'dontinclude'); structfun(F,A) ans = 3×1 logical array ·
Top answer 1 of 2
17
You can do this using an anonymous function that calls myfun with the two additional arguments:
cellfun(@(x) myfun(x,const1,const2), cellarray)
2 of 2
4
Another trick is to use ARRAYFUN on the indices:
arrayfun(@(k) myfun(cellarray{k},const1,const2), 1:numel(cellarray))
if the return values of myfun are not scalars, you might want to set the 'UniformOutput',false option.
Northwestern University
ece.northwestern.edu › local-apps › matlabhelp › techdoc › ref › struct.html
struct (MATLAB Functions)
Create structure array · Syntax s = struct('field1',{},'field2',{},...) s = struct('field1',values1,'field2',values2,...)
MathWorks
mathworks.com › matlab › language fundamentals › data types › structures
arrayfun - Apply function to each element of array - MATLAB
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.