Merge 2 text file in same row / same line

4 ビュー (過去 30 日間)
Farhan Hakimi Anuar
Farhan Hakimi Anuar 2020 年 6 月 20 日
Hi, i would like to combine 2 text file.
The data shown as below:
a1.txt
0:0 1:63 2:55 3:55
aa1.txt
4:54 5:56 6:61
I have try use code from below link:
%Combine Text File%
File = {'a1.txt', 'aa1.txt'}; % Thanks Walter
numberOfFiles = length(File); %
C = cell(1, numberOfFiles);
for k = 1:numberOfFiles
C{k} = strsplit(fileread(File{k}), '\n');
end
AllC = C{1};
for k = 2:numel(C)
AllC = strcat(AllC, {''}, C{k}); % [EDITED] With a space as separator here
end
[fid, msg] = fopen('a_fuse.txt', 'w');
if fid == -1, error('Cannot open file for writing: %s', msg); end
fprintf(fid, '%s\n', AllC{:});
fclose(fid);
the output will become as below:
0:0 1:63 2:55 3:55
4:54 5:56 6:61
BUT, i want the out put become as below:
0:0 1:63 2:55 3:55 4:54 5:56 6:61

回答 (1 件)

KSSV
KSSV 2020 年 6 月 20 日
編集済み: KSSV 2020 年 6 月 20 日
File = {'a1.txt', 'aa1.txt'};
N = length(File);
for i = 1:N
fid = fopen(File{i},'rt') ;
C = textscan(fid,'%s') ;
S{i} = C{1} ;
end
S = strjoin(S)
  4 件のコメント
Farhan Hakimi Anuar
Farhan Hakimi Anuar 2020 年 6 月 20 日
still can't same message as before.
File = {'a1.txt', 'aa1.txt'};
N = length(File);
for i = 1:N
fid = fopen(File{i},'rt') ;
C = textscan(fid,'%s') ;
S{i} = C{1} ;
end
S = strjoin(S)
C = cell(1, numberOfFiles);
for k = 1:numberOfFiles
C{k} = strsplit(fileread(File{k}), '\n');
end
AllC = C{1};
for k = 2:numel(C)
AllC = strcat(AllC, {''}, C{k}); % [EDITED] With a space as separator here
end
[fid, msg] = fopen('a_fuse.txt', 'w');
if fid == -1, error('Cannot open file for writing: %s', msg); end
fprintf(fid, '%s\n', AllC{:});
fclose(fid);
File = {'a1.txt', 'aa1.txt'};
Farhan Hakimi Anuar
Farhan Hakimi Anuar 2020 年 6 月 20 日
This how the data being arrange.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by