How to extract number of fields and name of fields from a structure?

398 views (last 30 days)
I have s.a = 1; s.b = 'abc'; s.c = []; How to extract the name of the fields: 'a', 'b', 'c'? And the number of fields: 3 (in this example)?

Answers (2)

kintali narendra
kintali narendra on 16 Jan 2018
use length(fieldnames(structure)) to find the number of fields in a structure. In your case it is length(fieldnames(s)). To get the fieldnames use fieldnames(structure) in your case fieldnames(s) store the output in a different variable and call them.

Image Analyst
Image Analyst on 22 Aug 2016
Use the fieldnames() function.

Categories

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!