Get number of fields? the result should be 14! I tried with size but it did not work!
1 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/697019/image.png)
0 个评论
回答(1 个)
Stephen23
2021-7-28
编辑:Stephen23
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 个评论
Stephen23
2021-7-28
编辑:Stephen23
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)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!