writing in a pre-existing txt file ?
Show older comments
Hello,
I have a question regarding writing my outputs to a pre-existing text file in a specific line
example :
in the text file I have the following :
A =
B =
I want my code to write the results next to A and B in the text file
The results will be ( for example ):
A = 1 2 3 4 5 6
B = 4 5 6 7 8 9
Can we do that in MATLAB ?
Thanks,
1 Comment
Walter Roberson
on 15 Jan 2020
Note that it is not possible in any operating system MATLAB is supported on, to append text to a line, except by rewriting everything from that point to the end of file. Only the IBM MVS version of MATLAB (we are talking decades ago!) could put text on the end of a line, and only for files initialized a particular way; the mechanism involved each line being fixed length. DEC's VMS is the only operating system that MATLAB was ever been supported on that really supported inserting variable amounts of text to the end of a line without rewriting to end of file and without the lines being fixed length -- but for technical reasons, MATLAB never supported that particular kind of VMS file.
In all file systems and operating systems after the two I mention above, files are represented as streams of bytes, and the only way to add to a line is to copy the entire rest of file .
Accepted Answer
More Answers (1)
Akira Agata
on 15 Jan 2020
Assuming the pre-existing text file is data.txt, how about the following?
cInput = readcell('data.txt','Delimiter','\n');
cAppend = {'1 2 3 4 5 6';'4 5 6 7 8 9'};
cOutput = strcat(cInput,cAppend);
writecell(cOutput,'data.txt')
5 Comments
Mahmoud Khadijeh
on 24 May 2021
Walter Roberson
on 24 May 2021
Did you edit the original question? Or do the files really only contain the text
A =
B =
and if they do contain only that, are there fixed numbers being appended? Or does the file contain the numbers?
Which MATLAB release are you using?
Mahmoud Khadijeh
on 24 May 2021
Akira Agata
on 24 May 2021
The following error message you encountered is due to your MATLAB version (R2016a).
The readcell function has been introduced in R2019a.
> Undefined function or variable 'readcell'.
Mahmoud Khadijeh
on 25 May 2021
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!