Get number of fields? the result should be 14! I tried with size but it did not work!
1 view (last 30 days)
Show older comments
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/697019/image.png)
0 Comments
Answers (1)
Stephen23
on 28 Jul 2021
Edited: Stephen23
on 28 Jul 2021
"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 Comments
Stephen23
on 28 Jul 2021
Edited: Stephen23
on 28 Jul 2021
"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)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!