modifying variable names to enable indexing in a loop

3 次查看(过去 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 . . . .

采纳的回答

Bob Thompson
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
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.
William Campbell
William Campbell 2021-4-27
Hello Bob,
thanks again for your comments - I have just found out my problem : "Curly brackets" !
I didn't place curly brackets around the cell array in my structure in the lower loop !
and with some changes in indices within the script I now have a formatted dataset.
Thanks for all your comments - it has kept me engaged this evening for the last 5 hours and produced the right outcome. Thanks again.
radar(j).object(r).position = [ob1{r}.data(1,j); ob2{r}.data(1,j); ob3{r}.data(1,j)];

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by