- Create a new file.
- Write the new data to the file.
- Read the old file and keep appending the data to the new file.
how to add 5 rows in a txt file
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
Tony Mohan Varghese
am 23 Okt. 2017
To write data to the top of the file, it is better to create a new file and write the required data to it.
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);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!