Prev: help
Next: piexel counting of HSV image
From: Reza Zare on 22 Jul 2010 04:50 Hello All, I have a text file( sample.txt) containing some numbers, I wana write all the similar number into another file and delete them from original file? pls advise me on this... my code is as follow: for this case, first, fourth and fifth line are having similar number, so I have written this three numbers into another file, how can I delete them from the sample.txt. sample.txt ********************** 1880 1121-127-700-500 1882 1121-115-710-400 1891 1121-115-700-400 1892 1121-127-700-500 1893 1121-127-700-500 ... ********************** for i=1:12 fid=fopen('sample.txt'); c=textscan(fid,'%s %s'); a=c{2}{1}; a1=c{2}{i}; tf=isequal(a,a1); if (tf==1) fid1=fopen('class1.txt','a+'); fwrite(fid1, c{1}{i}); end end fclose(fid1); fclose(fid);
From: Nic Roberts on 22 Jul 2010 06:58 One option would be to open another file omitting the unwanted data. You could then delete th old file and rename the new one. Nic "Reza Zare" <persianfriend12(a)yahoo.com> wrote in message <i290o4$c6p$1(a)fred.mathworks.com>... > Hello All, > > I have a text file( sample.txt) containing some numbers, I wana write all the similar number into another file and delete them from original file? pls advise me on this... my code is as follow: for this case, first, fourth and fifth line are having similar number, so I have written this three numbers into another file, how can I delete them from the sample.txt. > > sample.txt > ********************** > 1880 1121-127-700-500 > 1882 1121-115-710-400 > 1891 1121-115-700-400 > 1892 1121-127-700-500 > 1893 1121-127-700-500 > ... > > ********************** > for i=1:12 > fid=fopen('sample.txt'); > c=textscan(fid,'%s %s'); > a=c{2}{1}; > a1=c{2}{i}; > tf=isequal(a,a1); > if (tf==1) > fid1=fopen('class1.txt','a+'); > fwrite(fid1, c{1}{i}); > end > end > fclose(fid1); > fclose(fid);
From: Reza Zare on 22 Jul 2010 13:12 "Reza Zare" <persianfriend12(a)yahoo.com> wrote in message <i290o4$c6p$1(a)fred.mathworks.com>... > Hello All, > > I have a text file( sample.txt) containing some numbers, I wana write all the similar number into another file and delete them from original file? pls advise me on this... my code is as follow: for this case, first, fourth and fifth line are having similar number, so I have written this three numbers into another file, how can I delete them from the sample.txt. > > sample.txt > ********************** > 1880 1121-127-700-500 > 1882 1121-115-710-400 > 1891 1121-115-700-400 > 1892 1121-127-700-500 > 1893 1121-127-700-500 > ... > > ********************** > for i=1:12 > fid=fopen('sample.txt'); > c=textscan(fid,'%s %s'); > a=c{2}{1}; > a1=c{2}{i}; > tf=isequal(a,a1); > if (tf==1) > fid1=fopen('class1.txt','a+'); > fwrite(fid1, c{1}{i}); > end > end > fclose(fid1); > fclose(fid);
From: Reza Zare on 22 Jul 2010 13:15 and that's my question, how to omit an unwanted data, whether in the original file or a new file, it does not matter, what my question is that how to delete those data? "Reza Zare" <persianfriend12(a)yahoo.com> wrote in message <i290o4$c6p$1(a)fred.mathworks.com>... > Hello All, > > I have a text file( sample.txt) containing some numbers, I wana write all the similar number into another file and delete them from original file? pls advise me on this... my code is as follow: for this case, first, fourth and fifth line are having similar number, so I have written this three numbers into another file, how can I delete them from the sample.txt. > > sample.txt > ********************** > 1880 1121-127-700-500 > 1882 1121-115-710-400 > 1891 1121-115-700-400 > 1892 1121-127-700-500 > 1893 1121-127-700-500 > ... > > ********************** > for i=1:12 > fid=fopen('sample.txt'); > c=textscan(fid,'%s %s'); > a=c{2}{1}; > a1=c{2}{i}; > tf=isequal(a,a1); > if (tf==1) > fid1=fopen('class1.txt','a+'); > fwrite(fid1, c{1}{i}); > end > end > fclose(fid1); > fclose(fid);
From: Walter Roberson on 22 Jul 2010 13:43
Reza Zare wrote: > and that's my question, how to omit an unwanted data, whether in the > original file or a new file, it does not matter, what my question is > that how to delete those data? Just don't write them to the new file. Deleting data "in place" in an existing file is often not possible without unacceptable consequences. Creating a new file that only holds the things you _do_ want is a much less risky mechanism. |