Thread Subject: textscan - 2 rows of data per record

Subject: textscan - 2 rows of data per record

From: Pete sherer

Date: 13 Mar, 2009 20:47:01

Message: 1 of 2

I have a text file that each data contains 2 rows of information
900,id,name,age,height,sex
901,weight,occupation

Example:

900,1,Jim,60,175,male
901,150,none
900,2,Paul,30,190,male
901,233,engineer

With this non-uniform data, can I use TEXTSCAN to read the data above?

Subject: textscan - 2 rows of data per record

From: Doug Schwarz

Date: 14 Mar, 2009 03:40:50

Message: 2 of 2

In article <gpego5$n8k$1@fred.mathworks.com>,
 "Pete sherer" <tsh@abg.com> wrote:

> I have a text file that each data contains 2 rows of information
> 900,id,name,age,height,sex
> 901,weight,occupation
>
> Example:
>
> 900,1,Jim,60,175,male
> 901,150,none
> 900,2,Paul,30,190,male
> 901,233,engineer
>
> With this non-uniform data, can I use TEXTSCAN to read the data above?

Yes, with something like this:

>> fid = fopen('pete.dat');
>> c = textscan(fid,'%*n%n%s%n%n%s%*n%n%s',...
'Delimiter',',\n','Whitespace','');
>> fclose(fid);
>> c{1}
ans =
     1
     2
>> c{2}
ans =
    'Jim'
    'Paul'
>> c{3}
ans =
    60
    30
>> c{4}
ans =
   175
   190
>> c{5}
ans =
    'male'
    'male'
>> c{6}
ans =
   150
   233
>> c{7}
ans =
    'none'
    'engineer'

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

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com