Combining multiple CSV's from a list of csv pairs

1 view (last 30 days)
Jorge Young
Jorge Young on 30 Jun 2022
Edited: Jon on 30 Jun 2022
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?

Answers (1)

Jon
Jon on 30 Jun 2022
Edited: Jon on 30 Jun 2022
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

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!