필터 지우기
필터 지우기

modifying variable names to enable indexing in a loop

조회 수: 4 (최근 30일)
William Campbell
William Campbell 2021년 4월 27일
댓글: William Campbell 2021년 4월 27일
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