フィルターのクリア

Is there a way to extract the name of a field of a struct as string

142 ビュー (過去 30 日間)
Omer Yasin Birey
Omer Yasin Birey 2019 年 2 月 15 日
コメント済み: Stephen23 2019 年 2 月 15 日
Hi all, lets say I have this struct 'str50' and this struct consists of 3 fields and their names are V10, V100, V1000. I want to extract the name of the field as string. I tried inputname() but it didn't work
inputname(str50.V10)
So I want to get the output as below
ans =
V10
Thank you!
  4 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 15 日
I am not certain what you want , perhaps
str50.('V10') %?
Stephen23
Stephen23 2019 年 2 月 15 日
"Is there a way to extract the name of a field of a struct as string"
Yes, using fieldnames. But your example shows some confusion about the topic: although you ask in your title how to extract the fieldname of a structure, the syntax S.F refers to the contents of structure S's field F, i.e. another array (which may or may not be an unrelated structure) but that other array certainly not the same thing as S, so it does not know anything about the fieldnames of S.
It is as if you put an array into a cell, then take it out of the cell and want the array to somehow "know" that it had just been stored in a cell array. MATLAB does not store such information.

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

回答 (1 件)

Stephan
Stephan 2019 年 2 月 15 日
編集済み: Stephan 2019 年 2 月 15 日
Hi,
the following gives a string array with the fieldnames of the struct:
A.a123 = 1
A.bbb = 2
B = string(fieldnames(A))
pick the needed field by indexing it or by logical indexing:
C = B(2)
D = B(B=="bbb")
or use:
B = fieldnames(A)
C = string(B{2})
Best regards
Stephan

カテゴリ

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