modifying variable names to enable indexing in a loop
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
William Campbell
le 27 Avr 2021
Commenté : William Campbell
le 27 Avr 2021
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 commentaires
Réponse acceptée
Bob Thompson
le 27 Avr 2021
Modifié(e) : Bob Thompson
le 27 Avr 2021
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 commentaires
Bob Thompson
le 27 Avr 2021
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!