Copying specific lines from multiple cvs file to a single file

조회 수: 1 (최근 30일)
Stell123
Stell123 2020년 3월 2일
답변: Anmol Dhiman 2020년 3월 6일
Hello,
I have this code:
%% Merge multiple cvs files into one file - open current folder, select files to merge
[filenames,folder]=uigetfile('*.csv','select the data file','multiselect','on')
%% creates output file name in the same folder
output= fullfile(folder,'trialcsvmerge.csv');
%%opens output file to write
fidOutput=fopen(output,'wt');
for k=1:3(filenames); %get this filename; enter number of files to merge
thisFileName=fullfile(folder,filenames{k});
fidInput=fopen(thisFileName); %open input files
thisText=fread(fidInput,'*char'); %%read text from file
fwrite(fidOutput, thisText); %copy to output file
fclose(fidInput); %close input file
end
The code works fine in combining multiple files into a single file, however it copies the titles and for whatever reason adds an extra empty line between all lines.. Is there a way to add a line that it just takes let say lines 2-10 from each file and copies just that, e.g. skipping the title?

답변 (1개)

Anmol Dhiman
Anmol Dhiman 2020년 3월 6일
Hi Steliana,
You can use readtable to read data from csv and then use join to merge data from all the csv files.
Thanks

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by