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

2 次查看(过去 30 天)

回答(1 个)

Stephen23
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
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 CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by