Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Delete single line using textscan

Subject: Delete single line using textscan

From: David Reed

Date: 1 Aug, 2008 20:41:03

Message: 1 of 6

I want to use textscan to bring in a whole bunch of data,
almost 50000 lines of it. Everything is formatted nicely
except for a few lines here and there.

I need a way for text scan to not read these lines. The
problem is that they start like the others so I can't use
commentstyle.

Does anyone have any ideas. Thanks

Subject: Delete single line using textscan

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 1 Aug, 2008 20:54:12

Message: 2 of 6

In article <g6vscv$91j$1@fred.mathworks.com>,
David Reed <dreed@mitre.org> wrote:
>I want to use textscan to bring in a whole bunch of data,
>almost 50000 lines of it. Everything is formatted nicely
>except for a few lines here and there.

>I need a way for text scan to not read these lines. The
>problem is that they start like the others so I can't use
>commentstyle.

>Does anyone have any ideas. Thanks

If you know exact line numbers, use counts on the textscan()
to read up to that point, then fgetl() to read the next
line (and discard it), then textscan() as much more as you can, etc..

If you don't know exact line numbers and can only tell by the
different formatting, you can either process a line at a time
(using fgetl to get the line and then one of the several parsing
routines), or you can read the entire file at once as a string,
delete the lines that give the problems, then textscan() the
string variable. (You can put a string in, in place of a fid).

--
   "I was very young in those days, but I was also rather dim."
   -- Christopher Priest

Subject: Delete single line using textscan

From: AHMET ANIL DINDAR

Date: 2 Aug, 2008 07:32:39

Message: 3 of 6

Hi,
I've a nice script for evaulation of such files.

%%%--------------------- START %%%%%%%%%%
fid=fopen('filename.dat') ; % the original file
fidd=fopen('new filename.dat') ; % the new file
while ~feof(fid) ; % reads the original till last line
  tline=fgets(fid) ; %
     if "CONDITION STATEMENT THAT REPRESENTS THE LINES YOU
 WANT TO DELETE"
     else
       fprinft(fidd,tline) ;
     end
end
fclose all
%%%--------------------- END %%%%%%%%%%


This is a simple script lets you to evaulate each line and
take action.

Ahmet Anil Dindar

Subject: Delete single line using textscan

From: AHMET ANIL DINDAR

Date: 2 Aug, 2008 07:45:03

Message: 4 of 6

Correction to my code
say the file (ahmet.txt) includes the following lines

ahmet anıl dindar
10 20 30 40
10 20 30 40
10 20 30 40
end

the script for deletion the alphabetic lines is

%%%--------------------- START %%%%%%%%%%
fid=fopen('ahmet.txt') ; % the original file
fidd=fopen('new filename.dat','w') ; % the new file
while ~feof(fid) ; % reads the original till last line
  tline=fgets(fid) ; %
     if isletter(tline(1))==1 ;
     else
       fwrite(fidd,tline) ;
     end
end
fclose all ;
%%% -------------------END %%%%%%%%%%%%%%

the new file includes

10 20 30 40
10 20 30 40
10 20 30 40

I tested it and it worked.

AAD

Subject: Delete single line using textscan

From: Andres

Date: 2 Aug, 2008 10:15:05

Message: 5 of 6

"David Reed" <dreed@mitre.org> wrote in message
<g6vscv$91j$1@fred.mathworks.com>...
> I want to use textscan to bring in a whole bunch of data,
> almost 50000 lines of it. Everything is formatted nicely
> except for a few lines here and there.
>
> I need a way for text scan to not read these lines. The
> problem is that they start like the others so I can't use
> commentstyle.
>
> Does anyone have any ideas. Thanks

I don't know if this applies to your problem, but just as a
hint:
txt2mat from the file exchange has the option to ignore
lines in the text file that can be identified by one or more
distinct strings ('BadLineString', see the doc). It can also
import the whole file line-by-line, letting you sort out the
critical lines afterwards. Note that it is made for an
import of data resulting in a single numeric matrix. If you
need advice with txt2mat, you may contact me (the author)
via the file exchange 'Contact author' button.
Regards
Andres

Subject: Delete single line using textscan

From: Cy abd

Date: 24 Sep, 2008 02:19:01

Message: 6 of 6

"AHMET ANIL DINDAR" <adindar@iku.edu.tr> wrote in message <g7139v$8mp$1@fred.mathworks.com>...
> Correction to my code
> say the file (ahmet.txt) includes the following lines
>
> ahmet anıl dindar
> 10 20 30 40
> 10 20 30 40
> 10 20 30 40
> end
>
> the script for deletion the alphabetic lines is
>
> %%%--------------------- START %%%%%%%%%%
> fid=fopen('ahmet.txt') ; % the original file
> fidd=fopen('new filename.dat','w') ; % the new file
> while ~feof(fid) ; % reads the original till last line
> tline=fgets(fid) ; %
> if isletter(tline(1))==1 ;
> else
> fwrite(fidd,tline) ;
> end
> end
> fclose all ;
> %%% -------------------END %%%%%%%%%%%%%%
>
> the new file includes
>
> 10 20 30 40
> 10 20 30 40
> 10 20 30 40
>
> I tested it and it worked.
>
> AAD


AAD,
GREAT JOB, thank you. This is exactly what I was looking for.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us