开发者

Matlab: Names of the variables in a struct

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

For example:

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

I want the name of s(1), s(2) and s(3). In this case I would g开发者_运维知识库et 'hello', 'foo' and 'bar'.


You're looking for FIELDNAMES

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

Note that fn is a cell array, so you get the 'foo' as fn{2}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜