How to write/append a specific text line repetitively in MATLAB??
12 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Guillaume
2017-5-15
" 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.
更多回答(2 个)
Jan
2017-5-15
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 个评论
aram salehi
2018-5-23
编辑:aram salehi
2018-5-23
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!