开发者

Matlab: Names of the variables in a struct

开发者 https://www.devze.com 2023-02-19 17:02 出处:网络
Given a struct from a function with a number of unknown fields, how do I get the name of each field?

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

精彩评论

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