modifying variable names to enable indexing in a loop
4 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to access data within variables which have been named with their index as part of their name and want to access them in a ‘for’ loop with the loop index ‘k’ as part of the variable name ‘_Obj < k >’. Is there any way I can do this ?
Demo_data.Sensor_Obj0_VarX1.data
Demo_data.Sensor_Obj0_VarY1.data
Demo_data.Sensor_Obj0_VarX2.data
Demo_data.Sensor_Obj0_VarY2.data
etc . . . .
Demo_data.Sensor_Obj1_VarX1.data
Demo_data.Sensor_Obj1_VarY1.data
Demo_data.Sensor_Obj1_VarX2.data
Demo_data.Sensor_Obj1_VarY2.data
etc . . . .
Demo_data.Sensor_Obj2_VarX1.data
Demo_data.Sensor_Obj2_VarY1.data
Demo_data.Sensor_Obj2_VarX2.data
Demo_data.Sensor_Obj2_VarY2.data
etc . . . .
0 个评论
采纳的回答
Bob Thompson
2021-4-27
编辑:Bob Thompson
2021-4-27
Adjust your variable to index into a multi-dimensional array. I recommend using the first dimension, rows, for the final number (x1, x2, x3, x4, etc.), the second dimension, columns, for x and y, and the third dimension, sheets, for the object number.
This does require that all objects contain the same number of x and y pairs.
Demo_data.Sensor(1,1,1).data
Demo_data.Sensor(1,2,1).data
Demo_data.Sensor(2,1,1).data
Demo_data.Sensor(2,2,1).data
etc . . . .
Demo_data.Sensor(1,1,2).data
Demo_data.Sensor(1,2,2).data
Demo_data.Sensor(2,1,2).data
Demo_data.Sensor(2,2,2).data
etc . . . .
Demo_data.Sensor(1,1,3).data
Demo_data.Sensor(1,2,3).data
Demo_data.Sensor(2,1,3).data
Demo_data.Sensor(2,2,3).data
Depending on the size, class, and consistency of data for each point, you might also be able to make the setup go to even more dimensions to encompass the data array indices. That would reduce one level of your structure, which will make it much easier to deal with.
5 个评论
Bob Thompson
2021-4-27
So, as a general rule, stacking structures is not a great idea, because it severely limits what kind of indexing you can call. Somebody explained it to me in detail once, I think it might have been Walter, but I don't remember the technical side. The point being, if you didn't have the extra layers, you should be able to define all of these at the same time, instead of individually in a loop. It honestly may be worth reworking a lot of the variables in the code, though I know that is probably a pain to think about.
Can you please post the specific error message you're getting, and point to which line is causing the error? I tried testing your code with some rough inputs and I didn't have a problem. You should be able to define the full bottom layer of a full structure stack, if you're only looking at one element of all other stack layers, which you seem to be.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!