How to combine multiple csv into 1 csv file?
조회 수: 166 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (3개)
Image Analyst
2016년 5월 22일
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
Walter Roberson
2016년 6월 11일
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
2016년 6월 11일
편집: dpb
2016년 6월 11일
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... :)
dpb
2016년 5월 22일
편집: dpb
2016년 5월 22일
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
Image Analyst
2022년 10월 24일
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
2022년 10월 25일
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.
Md Adil
2022년 11월 16일
편집: dpb
2022년 11월 16일
%% 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
Image Analyst
2022년 11월 16일
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:
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!