How to find the name of the first filed of a struct?
조회 수: 45 (최근 30일)
이전 댓글 표시
I have a structre which has some fields. How can I find the field name of the first field?
Here is the struct I have:
struct with fields:
unpatterned_H_65_full: [17×34 table]
unpatterned_H_100_full: [17×34 table]
unpatterned_H_100_ribboned: [17×34 table]
unpatterned_H_100_ribboned_sectioned: [17×34 table]
unpatterned_H_65_sectioned: [17×34 table]
unpatterned_He_100_sectioned: [17×34 table]
I need to write a code that returns say " unpatterned_H_65_full".
댓글 수: 0
채택된 답변
dpb
2020년 2월 9일
If the struct variable is s, then
name=fieldnames(s);
name=name(1);
No way to write the second-level addressing w/o the intermediary/temporary variable comes to me for a one-liner.
댓글 수: 4
Walter Roberson
2020년 2월 9일
Having names around permits you to examine additional fields less expensively.
In any case fieldnames returns a cell array of character vectors and indexing it with (1) would return a scalar cell instead of the character vector
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!