# Get number of fields? the result should be 14! I tried with size but it did not work!

조회 수: 2(최근 30일)
Paul Hinze 2021년 7월 28일
편집: Stephen 2021년 7월 28일

댓글을 달려면 로그인하십시오.

### 답변(1개)

Stephen 2021년 7월 28일
편집: Stephen 2021년 7월 28일
"I tried with size but it did not work!"
The size of a structure is completely independent of how many fields it has.
To count how many fields a structure has you can use this (where S is the structure):
numel(fieldnames(S))
"the result should be 14!"
Judging by your screenshot the structure has 14 elements, which you can count very simply:
numel(S)
Your screenshot does not show how many fields your structure has, but given that you expect the result to be 14 and that the structure (apparently) has 14 elements, it seems very likely that you are mixing up fields and elements of the structure. How many elements a structure has (and more specifically what sizes the dimensions have) is independent of how many fields a structure has (which is what you asked about).
##### 댓글 수: 5표시숨기기 이전 댓글 수: 4
Stephen 2021년 7월 28일
"I tried this, but still it gives me an error:"
No, you tried something else.
Your screenshot shows a structure vas_cell{1,1}.pleasantness, to which you could certainly apply the methods I showed in my answer. But instead you tried to index into that structure using a variable (or perhaps function) named sound:
vas_cell{1,1}.pleasantness(sound)
% ^^^^^^^ what do you expect this to do?
If the variable sound exists and is a valid set of indices then this will return the corresponding structure elements in a new structure (i.e. basic MATLAB indexing). But given the coincidence of the fieldname sound, I suspect that this is a syntactical mistake. It is not clear to me what you are trying to achieve by adding those parentheses.
Perhaps you want this:
numel(vas_cell{1,1}.pleasantness)

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by