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

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by