IDL:使用存储在变量中的字段名称访问结构字段?

发布于 2024-09-01 04:28:55 字数 156 浏览 3 评论 0原文

如果我有一个字段名为“fieldname”的结构,是否可以仅使用变量访问该字段中的数据?

IE。

x = 'fieldname'

是否可以以某种方式执行

data = struct.(x) ?我想使用 x 中的字符串作为字段名称。

If I have a struct with a fieldname 'fieldname', is it possible to access the data in that field using only the variable?

ie.

x = 'fieldname'

is it possible to do

data = struct.(x) in some way? I want to use the string in x as the field name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-09-08 04:28:55

是的,可以使用 TAG_NAMES 函数实现:

tnames=TAG_NAMES(struct)
tindex=WHERE(STRCMP(tnames,'fieldname') EQ 1)
data=struct.(tindex)

调用 TAG_NAMES 返回一个字符串数组,表示 struct 中定义的标签。
WHERE 语句返回与 'fieldname' 匹配的字符串在 tnames 中的索引。
最后,索引被传递给 struct.(tindex) 操作,该操作通过以下方式提取字段
它的数字标签索引。

当然,在真实的应用程序中,您需要检查 tindex 是否成功
与某些内容匹配,否则 IDL 将因索引的结构查找而阻塞
-1。

Yes, this is possible using the TAG_NAMES function:

tnames=TAG_NAMES(struct)
tindex=WHERE(STRCMP(tnames,'fieldname') EQ 1)
data=struct.(tindex)

The call to TAG_NAMES returns an array of strings representing the tags defined in struct.
The WHERE statement returns the index in tnames of a string matching 'fieldname'.
Finally, the index is passed to the struct.(tindex) operation, which extracts a field by
its numeric tag index.

Of course, in a real application you'd want to check whether tindex was successfully
matched to something, otherwise IDL will choke on the structure lookup with an index
of -1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文