MATLAB: How to find number of arrays in a structure filed

structstructures

Hello all,
Av(1).f1=[1 2 3 4]; Av(2).f1=[3 4 5 6]; Av(3).f1=[5 3 2 1]; Av(4).f1=[7 8 2 4];
Av(1).f2=[1 2 3 4 5]; Av(2).f2=[7 6 5 4 3];
I created a structure Av with two fields f1,f2.
I need to konw N1=number of arrays in Av.f1… N2=number of arrays in Av.f2…
So I tried
>> N=size(Av)
N =
1 4
>> N1=size(Av.f1)
??? Error using ==> size
Too many input arguments.
>> N2=size(Av.f2) ??? Error using ==> size Too many input arguments.
I have to get N1=4, N2=2
Please tell me how to calculate (N1 and N2) ?

Best Answer

Off the top of my head:
function [N1, N2] = fieldCount(inputStruct)
N1 = 0;
N2 = 0;
for i = 1: numel(inputStruct)
if(~isempty(getfield(inputStruct(i),'f1')))
N1 = N1 + 1;
end
if(~isempty(getfield(inputStruct(i),'f2')))
N2 = N2 + 1;
end
end
You can call this function as follows:
[N1, N2] = fieldCount(Av);