writing in a pre-existing txt file ?

조회 수: 19 (최근 30일)
Mahmoud Khadijeh
Mahmoud Khadijeh 2020년 1월 14일
댓글: Mahmoud Khadijeh 2021년 5월 25일
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
Walter Roberson
Walter Roberson 2020년 1월 15일
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 .

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

채택된 답변

Walter Roberson
Walter Roberson 2021년 5월 24일
S = fileread('YourFile.txt');
parts = regexp(S, '\r?\n', 'split');
if isempty(parts{end}); parts(end) = []; end %trailing newline
parts{1} = [parts{1} ' 1 2 3 4 5'];
parts{2} = [parts{2} ' 4 5 6 7 8'];
fid = fopen('NewFile.txt', 'wt');
fprintf('%s\n', parts{:});
fclose(fid);
It is not recommended that you write to the same file that is your input: if there were a problem in processing then you might have corrupted the original file.
As I discussed in January, it is not possible in any supported operating system to insert text at the end of a line without reading the entire rest of the file and rewriting it. All file systems on all systems supported by MATLAB are only streams of bytes that have no possible way to insert in place.
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 5월 24일
Sorry,
fprintf('%s\n', parts{:});
should have been
fprintf(fid, '%s\n', parts{:});
Mahmoud Khadijeh
Mahmoud Khadijeh 2021년 5월 25일
Thank You sir!
it works.
Regards,
Mahmoud

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

추가 답변 (1개)

Akira Agata
Akira Agata 2020년 1월 15일
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
Akira Agata
Akira Agata 2021년 5월 24일
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
Mahmoud Khadijeh 2021년 5월 25일
No problem.
Thank you Sir

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

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by