フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

marge two different types of .CSV files

2 ビュー (過去 30 日間)
ahasan ratul
ahasan ratul 2018 年 4 月 25 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I have two .csv files. 1st one has 1x134 strings in it called level.csv and 2nd one has 1000x134 numbers named sequences.csv. Can anyone help me to add these two .csv files into one? .csv file should have level as a first row and sequences from 2nd to 1000th row.
Thanks
Ahasan

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 4 月 25 日
S1 = regexprep( fileread('level.csv'), '\r?\n.*', ''); %trim off any CR/LF
S2 = regexp( fileread('sequences.csv'), '\r?\n', 'split' ); %break into lines
if isempty(S2{end}); S2(end) = []; end %if final line ended with newline then there is an extra empty line
output = [{S1}; S2]);
fid = fopen('combined.csv', 'wt');
fprintf(fid, '%s\n', output{:});
fclose(fid)
The above does not assume that the line terminators are the same for both files, and assumes that each file might end in a newline but might not. The combined file will be consistent.
In the case where you are certain that level.csv ends in a newline and that the two files are consistent in their line termination, then you can reduce all this to
!copy level.csv+sequences.csv output.csv

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by