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

1 Ansicht (letzte 30 Tage)
Paul Hinze am 28 Jul. 2021
Bearbeitet: Stephen23 am 28 Jul. 2021
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Stephen23 am 28 Jul. 2021
Bearbeitet: Stephen23 am 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 Kommentare4 ältere Kommentare anzeigen4 ältere Kommentare ausblenden
Stephen23 am 28 Jul. 2021
Bearbeitet: Stephen23 am 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)

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Structures 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!

Translated by