Matlab: Variable names in a structure

Given the structure of a function with a number of unknown fields, how do I get the name of each field?

For instance:

s = struct; s.hello = 'world'; s.foo = 12; s.bar = [ 1 2 3 ]; 

I need the name s (1), s (2) and s (3). In this case, I would get "hello", "foo" and "bar".

+4
source share
1 answer

Are you looking for FIELDNAMES

 fieldnames(s) fn = 'hello' 'foo' 'bar' 

Note that fn is an array of cells, so you get 'foo' as fn{2}

+6
source

Source: https://habr.com/ru/post/1345082/


All Articles