Got Questions? Get Answers.
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:
separating dataset in one file

Subject: separating dataset in one file

From: nman2084

Date: 23 May, 2011 21:29:04

Message: 1 of 8

If the file contains this format:

2.4
4.5
6.7
8.9
S
3.3
2.22
3.43
5.64

I need to make two data sets how do i read such a way that i can stop reading at S and start reading next data set.

                         

Subject: separating dataset in one file

From: TideMan

Date: 23 May, 2011 22:05:16

Message: 2 of 8

On May 24, 9:29 am, "nman2084 " <ng2...@yahoo.com> wrote:
> If the file contains this format:
>
> 2.4
> 4.5
> 6.7
> 8.9
> S
> 3.3
> 2.22
> 3.43
> 5.64
>
> I need to make two data sets how do i read such a way that i can stop reading at S and start reading next data set.

Probably the quickest way is to read the entire file in as string data
using textscan, then look through the cell array for the line starting
'S' using strmatch. Once you've found it, you can split the cell array
in two and convert to numeric data.
Have a go at coding this and we'll help debug it.

Subject: separating dataset in one file

From: nman2084

Date: 23 May, 2011 23:39:04

Message: 3 of 8

Thanks,
 I am facing problem with that S how do i stop reading until 'S'.

fid = fopen('testDat.txt');

k = 0;
while ~feof(fid)
    curr = textscan(fid,'%4f');
end
    
fclose(fid);

Subject: separating dataset in one file

From: dpb

Date: 23 May, 2011 23:51:49

Message: 4 of 8

On 5/23/2011 4:29 PM, nman2084 wrote:
> 2.4
> 4.5
> 6.7
> 8.9
> S
> 3.3
> 2.22
> 3.43
> 5.64

Do you know the number of entries a priori?

--

Subject: separating dataset in one file

From: dpb

Date: 24 May, 2011 00:11:05

Message: 5 of 8

On 5/23/2011 4:29 PM, nman2084 wrote:
> If the file contains this format:
>
> 2.4
> 4.5
> 6.7
> 8.9
> S
> 3.3
> 2.22
> 3.43
> 5.64
>
> I need to make two data sets how do i read such a way that i can stop
> reading at S and start reading next data set.

Oh, another idea struck...

If it's always a single character, make use of the fact that Matlab will
barf silently on the bad conversion...if it's not a fixed length string,
then Tideman's answer...

 >> fid=fopen('test.dat','rt');
 >> x=fscanf(fid,'%f')
x =
     2.4000
     4.5000
     6.7000
     8.9000
 >> fseek(fid,2,0); % NB 2 bytes for \n on Windows -- the 'S' was eaten
 >> y=fscanf(fid,'%f')
y =
     3.3000
     2.2200
     3.4300
     5.6400
 >> fid=fclose(fid);
 >> type 'test.dat'
2.4
4.5
6.7
8.9
S
3.3
2.22
3.43
5.64

>>

Subject: separating dataset in one file

From: dpb

Date: 24 May, 2011 00:16:52

Message: 6 of 8

On 5/23/2011 7:11 PM, dpb wrote:
...

> Oh, another idea struck...
>
> If it's always a single character, make use of the fact that Matlab will
> barf silently on the bad conversion...if it's not a fixed length string,
> then Tideman's answer...

Got to thinking as hit the "send" button...there's a cleaner way as the
file pointer didn't actually move when the conversion failed...

 >> x=fscanf(fid,'%f')
x =
     2.4000
     4.5000
     6.7000
     8.9000
 >> fgetl(fid)
ans =
S
 >> y=fscanf(fid,'%f')
y =
     3.3000
     2.2200
     3.4300
     5.6400
 >> fid=fclose(fid);
 >>

This way as long as it's a single line of text you should be golden.

--

Subject: separating dataset in one file

From: TideMan

Date: 24 May, 2011 01:05:55

Message: 7 of 8

On May 24, 12:16 pm, dpb <n...@non.net> wrote:
> On 5/23/2011 7:11 PM, dpb wrote:
> ...
>
> > Oh, another idea struck...
>
> > If it's always a single character, make use of the fact that Matlab will
> > barf silently on the bad conversion...if it's not a fixed length string,
> > then Tideman's answer...
>
> Got to thinking as hit the "send" button...there's a cleaner way as the
> file pointer didn't actually move when the conversion failed...
>
>  >> x=fscanf(fid,'%f')
> x =
>      2.4000
>      4.5000
>      6.7000
>      8.9000
>  >> fgetl(fid)
> ans =
> S
>  >> y=fscanf(fid,'%f')
> y =
>      3.3000
>      2.2200
>      3.4300
>      5.6400
>  >> fid=fclose(fid);
>  >>
>
> This way as long as it's a single line of text you should be golden.
>
> --

Here's my method:
fid=fopen('test.txt','rt');
c=textscan(fid,'%s');
fclose(fid);
indx=strmatch('S',c{1})
a=str2num(char(c{1}(1:indx-1)));
b=str2num(char(c{1}(indx+1:end)));

Subject: separating dataset in one file

From: dpb

Date: 24 May, 2011 01:16:39

Message: 8 of 8

On 5/23/2011 8:05 PM, TideMan wrote:
> On May 24, 12:16 pm, dpb<n...@non.net> wrote:
>> On 5/23/2011 7:11 PM, dpb wrote:
>> ...
...
>>> Oh, another idea struck...
...
>> Got to thinking as hit the "send" button...there's a cleaner way ...
>>
>> >> x=fscanf(fid,'%f')
>> >> fgetl(fid)
>> >> y=fscanf(fid,'%f')
>> >> fid=fclose(fid);
...
>
> Here's my method:
> fid=fopen('test.txt','rt');
> c=textscan(fid,'%s');
> fclose(fid);
> indx=strmatch('S',c{1})
> a=str2num(char(c{1}(1:indx-1)));
> b=str2num(char(c{1}(indx+1:end)));

Yeah, that, too... :)

If OP needs to actually verify it is an 'S' in the file, yours does
specifically--I just assumed some text although easy enough to test what
the fgetl() line returns.

Guess OP can choose... :)

--

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