append data to the already existed .txt file horizontally

9 views (last 30 days)
I want to append the data to my already existed .txt file, how can I do that.
My text file is like that:
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
and I want to add the data
5 6 7 8
5 6 7 8
5 6 7 8
5 6 7 8
so the whole text file should be something like that:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8

Answers (2)

KSSV on 15 Aug 2022
T1 = readtable('file1.txt') ;
T2 = readtable('file2.txt') ;
T = [T1 T2] ;

Sign in to comment.

Walter Roberson
Walter Roberson on 15 Aug 2022
If your files are already in .txt files, and they are in regular columns like you show, then the method that @KSSV shows will work fine.
You might perhaps be hoping to not have to read and interpret the text files and format back as text on output and writing out new files. If so we can break that up into two parts:
  1. can we avoid reading and writing the files?
  2. can we avoid intepreting as numeric and then formatting back to text again?
The answer to (1) is that something has to read and write files. The internal structure of text files on all supported operating systems is as continuous streams of characters, all of one line, then a line terminator, then all of the next line, line terminator, and so on. The line terminators are plain characters themselves: just linefeed character for MacOS and Linux, and potentially on Windows as well; carriage return character followed by linefeed character for older Windows programs. The file systems do not keep track of how long each line is, and the file systems do not have any way of saying "when you read in this file, add this segment on the end of line #83". The only thing the file systems support for text files is continuous streams of characters with no structure by the file system (other than to keep track of the length of the file.)
Because of this, if you want to append ' Hello' on the end of line 1 of a text input file, the best way is to read the first line, append ' Hello' to it, write that out to a new output file, then read lines from the input file and write them to that output file.
Is there an alternative to writing to a new file like this? Yes, there is if the revised file will not be smaller than the original file: you can read the first line, and insert ' Hello' into an output buffer, then start a process of reading ahead and adding to the output buffer and writing out from the output buffer only as many characters as you just read in. For example if you read 20 characters then you would write out ' Hello' followed by the next 14 characters, leaving the last 6 characters in the output buffer for the next cycle. Eventually you get to the end of the input file, and you write out the rest of the output buffer. But... if something goes wrong with this update-in-place process then you have ruined the file and cannot get it back. And notice that the total reading and writing characters is the same as for the write-to-new-file case. With the necessary buffer management to be sure that you do not accidentally overwrite something, the operations can turn out slower.
Does the "something" that does the reading and writing of the files have to be MATLAB? Well, if you are using MacOS or Linux, you can call upon the system utility paste to do the work; see
This leads to question (2), whether we can avoid interpreting as numeric and then refomatting again. The answer to that is Yes, if you are willing to do the text management discussed earlier, or if you can call paste or equivalent.
Are there other text ways to handle this? Yes,
L1 = readlines('file1.txt');
L2 = readlines('file2.txt');
newLines = strcat(L1, {' '}, L2);
writelines(newLines, 'OutputFileNameGoesHere.txt');
It is important that the ' ' be inside {} for the purposes of strcat. If you try to strcat(L1, ' ', L2) you will not get the right answer!
MakM on 16 Aug 2022
Database save all the columns at the same time, what I am trying to do is to shift the database data into some kind of text files or any other format so the data extraction should be faster.

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!