Genvarname for array of structs with indexing
2 次查看(过去 30 天)
显示 更早的评论
I have an array of structures, say AB. I operate upon it by using the nomenclature: AB(1).somefield or AB(5).someotherfield, etc. I now find I have to expand AB further. I would prefer to programmatically get to AB1(1).somefield or AB2(5).someotherfield, etc, taking the previous examples.
However, it looks as though I can't write:
(genvarname(['AB' num2str(index)]))(differentindex).somefield
Here, the variable 'index' would help generate various struct names (AB1, AB2, AB3, ...). 'differentindex' would be used to index into the array of structs.
I suppose I could instead expand the dimension of AB, and go with AB(x,y), but I don't prefer this. If there were an alternative I'd appreciate it, or if there were affirmation that AB(x,y) is the way to go, that's good to know as well.
0 个评论
采纳的回答
Steven Lord
2023-1-13
Can you dynamically create variables with numbered names like AB1, AB2, AB3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.
更多回答(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!