Select Structure Field Based on Variable
18 次查看(过去 30 天)
显示 更早的评论
Let's say I have a structure with multiple fields structured like this:
Structure.A = 5;
Structure.B = 10;
Structure.C = 30;
If I have a variable:
Field = "B"
Can I select the field B in the structure and set it equal to a new value? When I run this code, it creates a new field called "Field" instead of selecting the existing one from the value of Field.
Structure.Field = 15;
采纳的回答
Bhaskar R
2022-9-19
There is special notation to access structure or class field using parenthesis () of input string
Structure.A = 5;
Structure.B = 10;
Structure.C = 30;
Field = "B"
Structure.(Field) = 15 % this updates required field without adding extra field
0 个评论
更多回答(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!