From: Guangping Zhang on
1X1 structure S contain M1, M2, M3....M30 which has various length, such as 54, 32, 61...42.
I write code to get the length of each M.
If I write
fl(1)=length(S.(fS{1}));
fl(2)=length(S.(fS{2}));
fl(3)=length(S.(fS{3}));
..........
I will get correct length.
But if I using for loop below, answer is wrong, all equal 0.

fn=fieldnames(S); % all the field name was put into cell fn;
n=length(fn); % get fn length;
for i=1:n %get each Mi's length;
fl(i)=length(S.(fS{i}));
end

WHo can help me?
From: Walter Roberson on
Guangping Zhang wrote:
> 1X1 structure S contain M1, M2, M3....M30 which has various length, such
> as 54, 32, 61...42.
> I write code to get the length of each M. If I write
> fl(1)=length(S.(fS{1}));
> fl(2)=length(S.(fS{2}));
> fl(3)=length(S.(fS{3}));
> ..........
> I will get correct length.
> But if I using for loop below, answer is wrong, all equal 0.
>
> fn=fieldnames(S); % all the field name was put into cell fn;
> n=length(fn); % get fn length;
> for i=1:n %get each Mi's length;
> fl(i)=length(S.(fS{i}));
> end
>
> WHo can help me?

In your loop, your reference to fS should be a reference to fn .
From: Guangping Zhang on
Thank you so much, I fixed it out.

Yours,
Guangping,
From: Jos (10584) on
"Guangping Zhang" <zgp480(a)gmail.com> wrote in message <hoom1m$or4$1(a)fred.mathworks.com>...
> 1X1 structure S contain M1, M2, M3....M30 which has various length, such as 54, 32, 61...42.
> I write code to get the length of each M.
> If I write
> fl(1)=length(S.(fS{1}));
> fl(2)=length(S.(fS{2}));
> fl(3)=length(S.(fS{3}));
> ..........
> I will get correct length.
> But if I using for loop below, answer is wrong, all equal 0.
>
> fn=fieldnames(S); % all the field name was put into cell fn;
> n=length(fn); % get fn length;
> for i=1:n %get each Mi's length;
> fl(i)=length(S.(fS{i}));
> end
>
> WHo can help me?

Beside the spelling error (fS instead of fn), take a look at STRUCT2CELL and CELLFUN:

fl = cellfun(@length,struct2cell(S))

hth
Jos
From: us on
"Jos (10584) "
> Beside the spelling error (fS instead of fn), take a look at STRUCT2CELL and CELLFUN:
>
> fl = cellfun(@length,struct2cell(S))
>
> hth
> Jos

let alone this critter...

help structfun;
% eg,
r=structfun(@numel,s);

us