Horizontal concatenating table with matrix/array in loop
조회 수: 9 (최근 30일)
이전 댓글 표시
Hi All,
I am running a loop and from each iteration in the loop, I am saving a couple of datasets separately. Essentially, these datasets are either a table (contain values and strings) or matrix/array. In case if there was no dataset, I synthetically created a zeros matrix. At the end of the loop, I want a table or matrix (I don't mind) where all values from each iteration of the loop are horizontally concatenated in their respective matrices/tables.
Is there any way to concatenating table with matrix or other way around and get the output at the end of the loop? Thank you in advance.
Below is my poorly written code:
RD = []; RD_AV = []; RD_FV_TL = []; RD_LV_CL = []; RD_LV_TL = [];
load('LC_duration_start_end_centre_Edge.mat');
P = 'F:\Data folders\Waymo_LC_Data\New_data_29-3-23\LCdata_correct_longitudinal\LCdata_correct_longitudinal';
S = dir(fullfile(P,'*.csv'));
d = table();
fileNames = dir('*.csv');
for i = 1:numel(S)
F = fullfile(P,S(i).name);
d = readtable(F);
v_id = unique(d.VehID(d.vehtype=="av",:));
dat = LCstartendDuration([LCstartendDuration.veh_id(:)==v_id],:);
d_AV = d(d.vehtype=="av",:); d_FV_TL = d(d.vehtype=="tgtFollower",:);
d_LV_TL = d(d.vehtype=="tgtLeader",:); d_LV_CL = d(d.vehtype=="OrgLeader",:);
d_AV = d_AV(1:dat.start_centre,:);
d_AV.acc = (gradient(d_AV.speed))./0.1;
d_AV.dec = zeros(height(d_AV),1); d_AV.dec (end) = 1;
no = height(d_AV); rows = sort([no:-20:1, 1]); d_AV = d_AV(rows,:);
if isempty (d_FV_TL)
d_FV_TL = zeros(size(d_AV));
else
d_FV_TL = d_FV_TL(1:dat.start_centre,:);
d_FV_TL.acc = (gradient(d_FV_TL.speed))./0.1;
d_FV_TL = d_FV_TL(rows,:);
end
if isempty (d_LV_TL)
d_LV_TL = zeros(size(d_AV));
else
d_LV_TL = d_LV_TL(1:dat.start_centre,:);
d_LV_TL.acc = (gradient(d_LV_TL.speed))./0.1;
d_LV_TL = d_LV_TL(rows,:);
end
if isempty (d_LV_CL)
d_LV_CL = zeros(size(d_AV));
else
d_LV_CL = d_LV_CL(1:dat.start_centre,:);
d_LV_CL.acc = (gradient(d_LV_CL.speed))./0.1;
d_LV_CL = d_LV_CL(rows,:);
end
end
RD_AV = [RD_AV; d_AV]; RD_FV_TL = [RD_FV_TL; d_FV_TL];
RD_LV_CL = [RD_LV_CL; d_LV_CL]; RD_LV_TL = [RD_LV_TL; d_LV_TL];
댓글 수: 0
채택된 답변
Animesh
2023년 6월 2일
Hey @Yasir
You can do something like this:
for i = 1:numel(S)
% your code to read data and process it
% concatenate the processed data to the existing matrices/tables
RD_AV = horzcat(RD_AV, d_AV);
RD_FV_TL = horzcat(RD_FV_TL, d_FV_TL);
RD_LV_CL = horzcat(RD_LV_CL, d_LV_CL);
RD_LV_TL = horzcat(RD_LV_TL, d_LV_TL);
end
% you can now use RD_AV, RD_FV_TL, RD_LV_CL, RD_LV_TL as concatenated matrices/tables
You may refer to the following documentation for more information:
추가 답변 (1개)
Seth Furman
2023년 6월 5일
편집: Seth Furman
2023년 6월 5일
Sounds like you might want to use a tall table.
% Make some fake data in multiple *.csv files inside a folder named
% "my_files"
mkdir("my_files");
for i = 1:3
t = array2table(rand(2))
writetable(t, fullfile("my_files", i + ".csv"));
end
dir("my_files");
% Create a tall table from the "my_files" folder
parpool("Threads");
tallTbl = tall(datastore("my_files"))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Numeric Types에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!