How to combine multiple csv into 1 csv file?

282 views (last 30 days)
Kenny  Chintama
Kenny Chintama on 22 May 2016
Commented: Walter Roberson on 22 Oct 2021
Hello guys, I wanted to combine multiple csv files into one single file. Is it possible to do that? I have searched anywhere but it seems the code doesn't return any value or open microsoft excel at all...
Here's my code:
num = {};
text = {};
all = {};
p=dir('C:\Users\Kenny\Desktop\skripsi\pakebandingyangini\masuk\cowo\*.csv');
for i=1:length(p)
[num{end+1}, text{end+1}, all{end+1}]= xlsread(['C:\Users\Kenny\Desktop\skripsi\pakebandingyangini\masuk\cowo\', p(i).name]);
end
How to see the output of this code or, how can I join these csvs into 1 single csv file?
Thank you for your help.

Answers (2)

Image Analyst
Image Analyst on 22 May 2016
Read in the different csv files with csvread(). Then concatenate them and write them out with csvwrite()
csv1 = csvread(filename1);
csv2 = csvread(filename2);
csv3 = csvread(filename3);
allCsv = [csv1;csv2;csv3]; % Concatenate vertically
csvwrite(outputFileName, allCsv);
  5 Comments
dpb
dpb on 11 Jun 2016
I guess I sorta' knew that, Walter; thanks for posting it.
I moved from VAX/VMS to the micro world of embedded systems for a number of years before the PC and never have had occasion to use a -nix like OS...so, I couldn't have come up w/ the syntax prior to having seen it, but knew there just had to be one... :)

Sign in to comment.


dpb
dpb on 22 May 2016
Edited: dpb on 22 May 2016
TMW hasn't implemented much in the way of the OS inside Matlab; use the OS instead...
rootdir='C:\Users\Kenny\Desktop\skripsi\pakebandingyangini\masuk\cowo');
outname='allfiles'; % a new name for the output file
cmd=['copy ' fullfile(rootdir,'*.csv') ' ' fullfile(rootdir,outname,'.csv')]; % build OS COPY command
system(cmd) % and submit it to OS...
METHOD 2
Per my late awakening in sidebar conversation with IA, an "all Matlab" solution that is pretty efficient...
d=dir(fullfile(rootdir,'*.csv'); % retrieve the files
fido=fopen(fullfile(rootdir,'newout.csv'),'w'); % open output file to write
for i=1:length(d)
fidi=fopen(fullfile(rootdir,d(i).name)); % open input file
fwrite(fido,fread(fidi,'*char')); % copy to output
fclose(fidi); % close that input file
end
fido=fclose(fido); clear fid* % close output file, remove temporaries
  8 Comments
Walter Roberson
Walter Roberson on 22 Oct 2021
NumHeaders = 1;
% Merge multiple CSV files into one CSV file
[filenames, folder] = uigetfile('*.csv','Select the data file','MultiSelect','on'); % gets directory from any folder
if isnumeric(filenames); return; end %user cancel
filenames = cellstr(filenames); %in case user only selected one
% Create output file name in the same folder.
outputFileName = fullfile(folder, 'finalCSVnew.csv')
fidOutput = fopen(outputFileName, 'wt'); % open output file to write
for k = 1 : length(filenames)
% Get this file name.
thisFileName = fullfile(folder, filenames{k})
% Open input file:
fidInput = fopen(thisFileName);
%skip headers except for first file
if k ~= 1
for L = 1 : NumHeaders; fgetl(fidInput); end
end
% Read text from it
thisText = fread(fidInput, '*char');
% Copy to output file:
fwrite(fidOutput, thisText);
fclose(fidInput); % close the input file
end
fido=fclose(fidOutput);
% Open Windows Explorer to this folder
winopen(folder);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!