Naming a struct with a variable?
145 次查看(过去 30 天)
显示 更早的评论
Hi, I'm trying to sort some data into a struct by having a specific part of the struct name be variable. I have a mapping function that designates the name based on the index. This is a simplified version of what I'm trying to code:
AHAmap = [ 6 9 2 17]
S10.AHAmap(3) = 66;
My expectation being that the struct S10.2 equals 66 (or even better S10.AHA3 = 66. I've been stuck for a while so help is appreciated!
1 个评论
采纳的回答
Steven Lord
2020-3-18
2 isn't a valid struct array field name. But you can do something like your second alternative using dynamic field names.
S = struct();
FN = "apple" + 2 + "c"
S.(FN) = 42 % equivalent to S.apple2c = 42
Search the documentation for "dynamic field names" for more information.
更多回答(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!