Merging 2 or more Txt Files with same Column Number into a new Txt file

조회 수: 2 (최근 30일)
Tyann Hardyn
Tyann Hardyn 2021년 4월 23일
편집: Jan 2021년 5월 1일
Hi everyone.
I need to merge 2 or more files (.TXT) with a same column number but with a variation number of its row.
My Txt data are like these :
TXT File 1
2018 08 24 00 00 00 38413.08 405.01 -24481.97 38415.22 0.010543 -0.567395 45553.22
2018 08 24 00 00 01 38412.96 405.07 -24482.03 38415.10 0.010545 -0.567397 45553.15
2018 08 24 00 00 02 38412.96 405.01 -24481.97 38415.10 0.010543 -0.567396 45553.12
2018 08 24 00 00 03 38412.96 405.07 -24482.03 38415.10 0.010545 -0.567397 45553.15
2018 08 24 00 00 04 38413.15 405.01 -24482.03 38415.29 0.010543 -0.567395 45553.31
2018 08 24 00 00 05 38413.08 405.07 -24482.16 38415.22 0.010545 -0.567398 45553.32
TXT File 2
2018 08 27 00 00 00 38348.90 407.00 -24476.28 38351.06 0.010613 -0.568047 45496.07
2018 08 27 00 00 01 38349.02 407.00 -24476.34 38351.18 0.010613 -0.568047 45496.20
2018 08 27 00 00 02 38348.84 407.00 -24476.47 38351.00 0.010613 -0.568051 45496.12
2018 08 27 00 00 03 38348.71 407.00 -24476.47 38350.87 0.010613 -0.568053 45496.01
2018 08 27 00 00 04 38348.90 407.00 -24476.15 38351.06 0.010613 -0.568045 45496.00
2018 08 27 00 00 05 38348.71 407.07 -24476.34 38350.87 0.010615 -0.568050 45495.94
2018 08 27 00 00 06 38348.84 407.00 -24476.28 38351.00 0.010613 -0.568048 45496.02
2018 08 27 00 00 07 38348.71 406.88 -24476.15 38350.87 0.010610 -0.568047 45495.84
2018 08 27 00 00 08 38348.84 406.88 -24476.34 38351.00 0.010610 -0.568049 45496.05
So a new file that will become the output of Matlab Scipt is a Txt file that contain its merged data : TXT File 1 Data + TXT 2 File Data :
2018 08 24 00 00 00 38413.08 405.01 -24481.97 38415.22 0.010543 -0.567395 45553.22 ............ (TXT File 1 Data)
2018 08 24 00 00 01 38412.96 405.07 -24482.03 38415.10 0.010545 -0.567397 45553.15
2018 08 24 00 00 02 38412.96 405.01 -24481.97 38415.10 0.010543 -0.567396 45553.12
2018 08 24 00 00 03 38412.96 405.07 -24482.03 38415.10 0.010545 -0.567397 45553.15
2018 08 24 00 00 04 38413.15 405.01 -24482.03 38415.29 0.010543 -0.567395 45553.31
2018 08 24 00 00 05 38413.08 405.07 -24482.16 38415.22 0.010545 -0.567398 45553.32
2018 08 27 00 00 00 38348.90 407.00 -24476.28 38351.06 0.010613 -0.568047 45496.07 .............. (TXT FIle 2 Data below the TXT File 1 Data)
2018 08 27 00 00 01 38349.02 407.00 -24476.34 38351.18 0.010613 -0.568047 45496.20
2018 08 27 00 00 02 38348.84 407.00 -24476.47 38351.00 0.010613 -0.568051 45496.12
2018 08 27 00 00 03 38348.71 407.00 -24476.47 38350.87 0.010613 -0.568053 45496.01
2018 08 27 00 00 04 38348.90 407.00 -24476.15 38351.06 0.010613 -0.568045 45496.00
2018 08 27 00 00 05 38348.71 407.07 -24476.34 38350.87 0.010615 -0.568050 45495.94
2018 08 27 00 00 06 38348.84 407.00 -24476.28 38351.00 0.010613 -0.568048 45496.02
2018 08 27 00 00 07 38348.71 406.88 -24476.15 38350.87 0.010610 -0.568047 45495.84
2018 08 27 00 00 08 38348.84 406.88 -24476.34 38351.00 0.010610 -0.568049 45496.05
.............................................................................................................................................................. (Another TXT File Data which re still possible)
Could it be possible to form and run such a script in Matlab? Please help me. Thank you very much....

답변 (1개)

Jan
Jan 2021년 4월 23일
편집: Jan 2021년 5월 1일
This is easy in Matlab:
FileList = {'File1.txt', 'File2.txt', 'File3.txt'};
DestFile = fullfile(tempdir, 'JoinedFiles.txt');
JoinFiles(FileList, DestFile);
And the function:
function JoinFiles(FileList, DestFile)
FID = fopen(DestFile, 'W');
if FID < 0, error('Cannot open file: %s', DestFile); end
for iFile = 1:numel(FileList)
data = fileread(FileList{iFile});
fwrite(FID, data, 'char');
% Append trailing line break, if it is missing:
if ~endsWith(data, char(10))
fwrite(FID, char(10), 'char');
end
end
fclose(FID);
end
  댓글 수: 6
Tyann Hardyn
Tyann Hardyn 2021년 5월 1일
Im sorry Sir, but im still cant get the ouput file of that JoinedFiles... Please help me, Sir. Do u have some fixed or simple version of your script given to me before? Thank you very much...
Jan
Jan 2021년 5월 1일
편집: Jan 2021년 5월 1일
@Tyann Hardyn: I'm sorry also. I do not have any idea, why you cannot find the output file. I've explained, where you can find it. If should be trivial to define an output folder and find it in the file browser. Simply replace:
DestFile = fullfile(tempdir, 'JoinedFiles.txt');
by
DestFile = fullfile('D:\Choose\Like\You\Want', 'JoinedFiles.txt');
My code is really easy already and I do not see any way to simplify it.
Did you call the function I've provided anywhere? Of course you have to call a function to run it:
FileList = {'File1.txt', 'File2.txt', 'File3.txt'};
DestFile = fullfile('D:\Choose\Like\You\Want', 'JoinedFiles.txt');
JoinFiles(FileList, DestFile); % <== Call the function here

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by