Combining multiple CSV's from a list of csv pairs

조회 수: 1 (최근 30일)
Jorge Young
Jorge Young 2022년 6월 30일
편집: Jon 2022년 6월 30일
Thank you in advance for any help on this. I am not sure where to even start. I have a .csv which shows pairings of file names something like this, but with 1,000+ filepaths:
'C:File1.csv', 'C:File2.csv',
'C:File3.csv', 'C:File4.csv'
Each row in the .csv contains 2 .csv filepaths that are meant to go together. Is it possible to horizontally concatenate the data from each pair outlined in the "pairing .csv" into a new .csv?

답변 (1개)

Jon
Jon 2022년 6월 30일
편집: Jon 2022년 6월 30일
So for each row in the original "pairings.csv" you want to create a new .csv file, let's say for the 893rd row you would call this file out893.csv. The contents of this file would be created by reading in some matrix of values from each of the two files paths on the line, and horizontally concatenating them. You could do this as follows:
% read in the "pairing file"
pairings = readcell('pairings.csv','Delimiter',',');
% determine number of pairings
numPair = size(pairings,1);
% loop through pairings
for k = 1:numPair
% read in the data for each pair
% store in cell array since we don't know dimensions of the data to be
% read
data = cell(1,2); % preallocate cell to hold data
for j = 1:2
data{j} = readmatrix(pairings{k,j});
end
% now horizontally concatenate the data and write to the output file
outdat = horzcat(data{:,:});
outfile = "out"+num2str(k)+".csv";
writematrix(outdat,outfile)
end

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by