how to add 5 rows in a txt file

조회 수: 1 (최근 30일)
costanza sironi
costanza sironi 2017년 10월 18일
답변: Tony Mohan Varghese 2017년 10월 23일
Hello, I need to add some stuff to a txt file already containing a matrix. I need to add everything at the very beginning of the txt.
thank you costanza

답변 (1개)

Tony Mohan Varghese
Tony Mohan Varghese 2017년 10월 23일
To write data to the top of the file, it is better to create a new file and write the required data to it.
  1. Create a new file.
  2. Write the new data to the file.
  3. Read the old file and keep appending the data to the new file.
inputFile = fopen('currentFile.txt','r');
outputFile = fopen('newFile.txt','w+');
% create sample data to write to the new file
data = [ [1:10] ; [21:30] ];
fprintf(outputFile, '%5.3f %5.3f \n',data);
while true
line = fgetl(inputFile);
if ~ischar(line)
break;
end
fprintf(outputFile,'%s \n',line);
end
fclose(inputFile);
fclose(outputFile);

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by