Thread Subject: Scantext help

Subject: Scantext help

From: John Mastrangelo

Date: 18 Nov, 2009 00:01:03

Message: 1 of 2

Hi, I have a text file that I am trying to read using textscan. The file has a variable number of fields on each line and uses a colon as a deliminator. I am trying to get textscan to read a maximum number of fields ( easy) but I haven't figured out how to "stop" reading at the end of the line - if the line is short it starts reading from the next line which I don't want it to do. Any advice folks? thanks, John

Subject: Scantext help

From: Doug Schwarz

Date: 18 Nov, 2009 13:56:26

Message: 2 of 2

In article <hdvdfv$2p8$1@fred.mathworks.com>,
 "John Mastrangelo" <jmastrangelo.remove@remove.symmetricom.com.remove>
 wrote:

> Hi, I have a text file that I am trying to read using textscan. The file has
> a variable number of fields on each line and uses a colon as a deliminator. I
> am trying to get textscan to read a maximum number of fields ( easy) but I
> haven't figured out how to "stop" reading at the end of the line - if the
> line is short it starts reading from the next line which I don't want it to
> do. Any advice folks? thanks, John

If this is your data file:

1:2:3:4
1:2:3
1:2


Figure out the maximum number of items per line (4 in this case). Then
form your call to textscan as

  max_items = 4;
  fid = fopen('thefile.txt');
  fmt = repmat('&f',1,max_items); % '%f%f%f%f' for max_items = 4
  data = textscan(fid,fmt,'Delimiter',':');
  fclose(fid);

The missing items should be filled in with NaN.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
textscan John Mastrangelo 17 Nov, 2009 19:04:03
newline John Mastrangelo 17 Nov, 2009 19:04:03
rssFeed for this Thread

Contact us at files@mathworks.com