Saving data from nested loop into final table
1 回表示 (過去 30 日間)
古いコメントを表示
Hello all,
I have this code bellow and its repeated for 6 types of vehicles (the one shown bellow is for buses only), and I want to save data from inside the loop such as (frame_bus, local_time_bus, time_steps_bus, x_bus, y_bus, x_cent_bus, y_cent_bus) so the final table should have columns for each of those variables for all vehicles, something like:
column of "frame" = frame_bus + frame_car + frame_motor + frame_trailer + frame_truck + frame_van
column of local_time = local_time_bus + local_time_car + local_time_motor + local_time_trailer + local_time_truck + local_time_van
column of time_steps = time_steps_bus +time_steps_car +time_steps_motor + time_steps_trailer + time_steps_truck +time_steps_van
column of x_veh = x_bus + x_car + x_motor +x_trailer + x_truck + x_van
column of y_veh = y_bus + y_car +y_motor + y_trailer + y_truck + y_van
column of x_cent_veh = x_cent_bus + x_cent_car +x_cent_motor + x_cent_trailer + x_cent_truck +x_cent_van
column of y_cent_veh = y_cent_bus + y_cent_car +y_cent_motor + y_cent_trailer +y_cent_truck +y_cent_van
column ofveh_speed = speed_bus +speed_car + speed_motor +speed_trailer +speed_truck + speed_van
Thanks a lot in advance
%% Buses
Nbuses = length(busesId);
Nbuses_counted = 0;
for j = 1:Nbuses
buid_j = busesId(j);
busj = data2(data2.trackId == buid_j, :);
if size(busj, 1) == 0
continue;
end
% Get frame
frame_bus = busj.frame;
% frame_bus = [frame_bus];
% Get local_time
local_time_bus = busj.frame/25;
% local_time_bus = [local_time_bus];
% Get time steps
time_steps_bus = (busj.frame(2:end)-busj.frame(1:end-1))./25;
time_steps_bus = [0;time_steps_bus]; % add zero as the first local time step
% get xy points
x_bus = busj.xCenter;
y_bus = busj.yCenter;
% x_bus = [x_bus];
% y_bus = [y_bus];
% Get speed
speed_bus = sqrt(busj.xVelocity.^2 + busj.yVelocity.^2);
% speed_bus = [speed_bus];
% Get centroid
x_cent_bus = busj.width/2;
y_cent_bus = busj.length/2;
% x_cent_bus = [x_cent_bus];
% y_cent_bus = [y_cent_bus];
interacting_vehicles{v} = sprintf("veh %d", buid_j);
v = v + 1;
end
uniqueID{uid} = sprintf("R%dB%d", fid, bid);
uid = uid + 1;
end
0 件のコメント
採用された回答
Vijay
2022 年 11 月 14 日
Hi @Reem RA
You can use structure array to store the information and then use strcuct2table to convert them to table.
Please refer the below documentations for more information about structure arrays and struct2table.
Example for frame:
Frame = struct();
%create more structures for centroid, local_time, etc.
Inside the loop that is processing buses.
%… your code
if (isfield(Frame, iframe_bus ))
Frame.frame_bus = [Frame.frame_bus, frame_busj];
else
Frame.frame_bus = [frame_busj]];
end
Similarly, you can include this code into loops processing frames belonging to different vehicles. Just change the column name to other attribute like local_time, centroid, etc.
Hope this helps!
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!