How to find the name of the first filed of a struct?

62 ビュー (過去 30 日間)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2020 年 2 月 9 日
編集済み: dpb 2020 年 2 月 9 日
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".

採用された回答

dpb
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
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
dpb
dpb 2020 年 2 月 9 日
編集済み: dpb 2020 年 2 月 9 日
  1. Yes, if there were to be a need for other fields besides the stated need for only the first
  2. Yes, but cellstr is oftentimes more convenient than char string--but, sometimes it isn't...OP didn't give any context to know how the result was/is to be used. :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by