How to write/append a specific text line repetitively in MATLAB??

10 views (last 30 days)
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

Accepted Answer

Guillaume
Guillaume on 15 May 2017
" But I also want to have other machines to be updated by going back to respective text lines"
Going back into a file (text or binary) and appending data in the middle is never an easy operation. You have to bear in mind that a file is just one long linear sequence of character or bytes. For text, it means that
textline1______product1: machine2
textline2______product2: machine3
textline3______product3: machine1
is just written on disk as the continous sequence
textline1______product1: machine2\ntextline2______product2: machine3\ntextline3______product3: machine1
There is therefore no room to add anything at the end of machine2 and the only way to append data to the first line is to rewrite the whole file, overwriting the whole content. That's not going to be fast.
A possible workaround would be to add lots of blank spaces to each line when you first write them. Then when you need to append text to a line, you overwrite over some of the blank spaces. The downside is that you may waste a lot of space if some of these blanks are never filled. And of course, if you add too much and run out of blanks, then you're back to rewriting the whole file. Another downside is that you'd be moving the file pointer back and forth, an operation that's not particularly fast.
In my opinion, a better solution would be to change your approach and build the whole content of the file in memory, then once you're done write the whole file in one go.

More Answers (2)

Jan
Jan on 15 May 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.
  3 Comments
Jan
Jan on 15 May 2017
Edited: Jan on 15 May 2017
1 sentences,0 clarity
Neither the term "product" nor the number of "machines" matters. You want to insert strings in a text file and this is the problem.

Sign in to comment.


aram salehi
aram salehi on 23 May 2018
Edited: aram salehi on 23 May 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

Categories

Find more on Characters and Strings 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!