How to combine multiple csv into 1 csv file?

170 views (last 30 days)
Kenny  Chintama
Kenny Chintama on 22 May 2016
Edited: dpb on 16 Nov 2022
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 (3)

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
Walter Roberson
Walter Roberson on 11 Jun 2016
The use of '+' to concatenate multiple files into one is specific to MS Windows. In OS-X and Linux, the typical mechanism would be (at the shell level)
cat file1 file2 file3 > outputfile
dpb
dpb on 11 Jun 2016
Edited: 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.


Md Adil
Md Adil on 16 Nov 2022
Edited: dpb on 16 Nov 2022
%% Load in files
files_in_fold=dir('*.csv');
full_table=table;
for ii=1:length(files_in_fold)
temp=readtable(files_in_fold(ii).name);
for jj=1:height(temp)
name_table{jj,1}=files_in_fold(ii).name;
end
temp=[temp name_table];
full_table=[full_table;temp];
end
writetable(full_table,'Combined_CSVs.csv','WriteRowNames',true);
@Walter Roberson & @Image Analyst this code is working for me to combine the csv files with their names but the problem is it can't read more than 8 cells of csv file. If there is more than 8 cells, it can't read it. Can you please help me solving this problem?
  1 Comment
Image Analyst
Image Analyst on 16 Nov 2022
Yes, but how? You forgot to attach one of your CSV files, so how can we figure out why it's only reading part of it. Perhaps the format is messed up.
If you have any more questions, then attach your data in a new thread (not here) and code to read it in with the paperclip icon after you read this:

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
  17 Comments
Image Analyst
Image Analyst on 24 Oct 2022
I suggest you create a table with only 3 columns. Col 1 is the file name, col2 is Animal, and col3 is electrodes.
Walter Roberson
Walter Roberson on 25 Oct 2022
To get that structure, you will need to have your table have one variable per file (named after the file), and the variable would be a table with variables Animal and electrodes.
Otherwise you would need to use a cell array in which the first and second rows contained character vectors or strings or categorical, and the remaining rows contained numeric values. It is likely that the display would not be nice -- for example you could expect that 'electrodes' will be changed to '1x10 char' on display.
It cannot be done as a table() with first variable named 'file1.csv', third variable named 'file2.csv' and so on, because that would require naming the second and 4th columns with empty variable names, which is not permitted for table variables.

Sign in to comment.

Categories

Find more on Language Support in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!