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:
textscan problem

Subject: textscan problem

From: edward kabanyas

Date: 13 Feb, 2012 07:46:10

Message: 1 of 3

Hi all,

Previously I asked this question again and one of you suggest me to use textscan instead of textread. However, I still problem in reading the data correctly. The following is example of the data (for first minute only and only for few lines):

Dat 120112000000 UTC OMN 60 STP 150 ASL 120 SMP 125e3 SVS 6.0.0.2 DVS 6.00 DSN 99030001 CC 5477180 MDQ 100 TYP AVE
N05-596865-2.8e+6 1.7e+6 1.8e+6 4.6e+6 1.7e+6-2.7e+6-1.3e+6 1.9e+6-6.1e+6-3.9e+6 4.3e+6 1.1e+7 8.6e+6-8.0e+6 8.9e+6 2.0e+7 1.2e+7-1.1e+7 3.5e+6 1.8e+7 3.7e+7 4.9e+7 4.2e+7 1.4e+7 5.1e+6 2.2e+7 5.9e+7 1.1e+7 2.0e+6 1.8e+8
N06 203368-470324 226456-981101 546523 1.5e+6 -49857 1.2e+6 3.6e+6-520973 896222 5.4e+6 598828-1.3e+6-1.5e+6 7.4e+6 1.2e+7 1.0e+7-806060-976253 2.9e+6 4.3e+6 1.2e+7 1.2e+7 1.4e+6-4.2e+6 3.2e+6 1.1e+7-1.6e+7 1.4e+7 1.1e+8

I read the data by the following simple code:

fid = fopen('data.txt');
C = textscan(fid, '%3s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s');
fclose(fid);

Output:
C{1,1} = 'Dat' 'N06' 'N07'
C{1,2} = '-596865' '203368-'
C{1,3}= '-2.8e+6' '470324'

It can be seen that '203368-' must be '203368' (without -) and
                              '470324' must be '-470324'(with -)

If I read the data by using float type like: '%3s %f %f, it does not work because the third coulumn is character but if I use \%, same problem again occur. Probably you have experience with this kind of data reading, thanks for your share.

Edward

Subject: textscan problem

From: dpb

Date: 13 Feb, 2012 23:48:59

Message: 2 of 3

On 2/13/2012 1:46 AM, edward kabanyas wrote:
> Hi all,
>
> Previously I asked this question again and one of you suggest me to use
> textscan instead of textread. However, I still problem in reading the
> data correctly. The following is example of the data (for first minute
> only and only for few lines):
>
> Dat 120112000000 UTC OMN 60 STP 150 ASL 120 SMP 125e3 SVS 6.0.0.2 DVS
> 6.00 DSN 99030001 CC 5477180 MDQ 100 TYP AVE
> N05-596865-2.8e+6 1.7e+6 1.8e+6 4.6e+6 1.7e+6-2.7e+6-1.3e+6
> 1.9e+6-6.1e+6-3.9e+6 4.3e+6 1.1e+7 8.6e+6-8.0e+6 8.9e+6 2.0e+7
> 1.2e+7-1.1e+7 3.5e+6 1.8e+7 3.7e+7 4.9e+7 4.2e+7 1.4e+7 5.1e+6 2.2e+7
> 5.9e+7 1.1e+7 2.0e+6 1.8e+8
> N06 203368-470324 226456-981101 546523 1.5e+6 -49857 1.2e+6
> 3.6e+6-520973 896222 5.4e+6 598828-1.3e+6-1.5e+6 7.4e+6 1.2e+7
> 1.0e+7-806060-976253 2.9e+6 4.3e+6 1.2e+7 1.2e+7 1.4e+6-4.2e+6 3.2e+6
> 1.1e+7-1.6e+7 1.4e+7 1.1e+8
>
> I read the data by the following simple code:
>
> fid = fopen('data.txt');
> C = textscan(fid, '%3s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s
> %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s %7s
> %7s');
> fclose(fid);

I don't know why you would expect that to work at all, what more well.

You'll need to parse the first line separately from the other data
lines; do you need information from it and what, specifically, if so?

For the rest, try

fmt=['%3s' repmat('%8.1f',1,31)];
c=textscan(fid,fmt);

NB the "trick" of using repmat for building complicated format strings.

--

Subject: textscan problem

From: dpb

Date: 14 Feb, 2012 14:47:19

Message: 3 of 3

On 2/13/2012 5:48 PM, dpb wrote:
...

> For the rest, try
>
> fmt=['%3s' repmat('%8.1f',1,31)];
> c=textscan(fid,fmt);
>
> NB the "trick" of using repmat for building complicated format strings.

Oh, just noted the field width is 7, not 8. I used your 7 and a space
and assumed that was correct but it isn't.

Try
fmt=['%3s' repmat('%7.1f',1,31)];

instead (and check on the count).

Also, if the character id at the beginning isn't significant, you can
use the '*' modifier and skip the field as in

fmt=['%*3s' repmat('%7.1f',1,31)];

to return just the numeric data fields.

--

Tags for 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