How to write/append a specific text line repetitively in MATLAB??
Mostra commenti meno recenti
Hi,,
Actually i want to write some output to a text file.In this text file, at specific lines i have to add some results without deleting the previous results repetitively.
e.g. Suppose I have 3 products to be produced on a set of 2 machines like:Product{1,2,3};Machine{1,2}. Each product consists of 3 operations, to be processed on more than one machine in some sequence like:
product1: machine2 machine3 machine1
product2: machine3 machine1 machine2
product3: machine1 machine2 machine3
SAMPLE OUTPUT TEXT FILE(output file sorted on the basis of product number; product number=output text file line number)
textline1______product1: machine2 machine3 machine1
textline2______product2: machine3 machine1 machine2
textline3______product3: machine1 machine2 machine3
When the program executes it writes the processing info of the respective first machine products visit. But I also want to have other machines to be updated by going back to respective text lines(=product number) and add/append the existing info to the lines. The info to be added/appended in the respective lines is shown in the sample output text file in BOLD LETTERS.
I dont know how to go back to respective lines and add/append the text line.
Can anyone out there help me in this regard????
Any suggestions/link/tutorial etc will be great help.
Thanks
Risposta accettata
Più risposte (2)
Jan
il 15 Mag 2017
This is not possible. I think. I'm not sure if I understand your question correctly. This sentence is not clear to me:
3 products to be produced on a set of 2 machines like:Product{1,2,3};Machine{1,2}
But I assume you want to insert text in a text file at a specific location. Remember that a text file is a stream of characters on the hard disk. You cannot insert additional characters, only overwrite existing ones or append new characters. The only solution for text files is to import the complete text file to a cell string (fileread, strsplit), apply the modifications in the RAM and write the file back (at least the part from the beginning of the changes). This will be very inefficient if the text file ist large.
This is not the purpose of a text file. Prefer a log file design, which writes the changes sequentially and create the total file containing the sorted data after the procesing. Even better would be to use a database, because this would avoid collisions caused by a simultaneous access securely.
aram salehi
il 23 Mag 2018
Modificato: aram salehi
il 23 Mag 2018
hi you can use this function
text_selected=uigetfile('*.txt','Select txt file','MultiSelect','on');
for i=1:numel(text_selected)
fileID1 = fopen(text_selected{i});
A = fread(fileID1);
fclose(fileID1);
fileID=fopen('result_text.txt','a+');
fwrite(fileID,A);
fclose(fileID);
end
Categorie
Scopri di più su Text Files in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!