modifying variable names to enable indexing in a loop

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 . . . .

 Akzeptierte Antwort

Bob Thompson
Bob Thompson am 27 Apr. 2021
Bearbeitet: Bob Thompson am 27 Apr. 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 Kommentare

Jan
Jan am 27 Apr. 2021
Thanks. Avoid hiding indices in the names of variables.
Thanks for both your comments - actually I inherited the variable names, so that is why the variable indices are hidden in the names !
I tried putting things in a loop to create a structure and get an error :
Dot indexing is not supported for variables of this type.
However, when I type in lines manually in the workspace I can create an array structure without problems, for example :
>> radar{1}.object{9,1}.position = [];
the data contained within the ob<x> variables are defined above this program and are fine, it is just the problem when running these lines in a loop - I'm totally confused. What am I missing ?
for j = 1 : numel(scans);
numDets = nObj(:,j);
% radar{numel(scans),3}=[];
if numDets == 0
for s = 1:numDets;
radar(j).object(s,1).position = []; % [DistX; DistY;, DistZ]
radar(j).object(s,2).velocity = []; % [VrelX, VrelY, 0]
radar(j).object(s,3).acceleration = []; % ArelX
radar(j).object(s,4).range = []; % Dist
radar(j).object(s,5).rangeRate = []; % Vrel
radar(j).object(s,6).azimuth = []; % RelCourseAngle
radar(j).object(s,7).RCS = []; % RCS
radar(j).object(s,8).SNR = []; % SNR
radar(j).object(s,9).SignalStrength = []; % SignalStrength
radar(j).object(s,10).width = []; % Width
radar(j).object(s,11).widthClass = []; % WidthClass
radar(j).object(s,12).length = []; % Length
radar(j).object(s,13).lengthClass = []; % Length Class
radar(j).object(s,14).DynProp = []; % DynProp
end
else
for r = 1 : numDets;
radar(j).object(r,1).position = [ob1(r,1).data(1,j); ob2(r,1).data(1,j); ob3(r,1).data(1,j)];
radar(j).object(r,2).velocity = [ob4(r,1).data(1,j); ob5(r,1).data(1,j); 0]; % vz not measured
radar(j).object(r,3).RangeRate = ob6(r,1).data(1,j);
radar(j).object(r,4).range = ob7(r,1).data(1,j);
radar(j).object(r,5).acceleration = ob8(r,1).data(1,j);
radar(j).object(r,6).azimuth = ob9(r,1).data(1,j);
radar(j).object(r,7).RCS = ob10(r,1).data(1,j);
radar(j).object(r,8).SNR = ob11(r,1).data(1,j);
radar(j).object(r,9).SignalStrength = ob12(r,1).data(1,j);
radar(j).object(r,10).width = ob13(r,1).data(1,j);
radar(j).object(r,11).widthClass = ob14(r,1).data(1,j);
radar(j).object(r,12).length = ob15(r,1).data(1,j);
radar(j).object(r,13).lengthClass = ob16(r,1).data(1,j);
radar(j).object(r,14).DynProp = ob17(r,1).data(1,j);
end
end
end
Also, the following works better to create an array from the command line instead of the cell structure that I showed above - however, still doesn't run inside a loop !
>> radar(1).object(8,1).position = [];
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.
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)];

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by