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:
Error using textscan and headerlines

Subject: Error using textscan and headerlines

From: Katherine

Date: 28 Mar, 2011 15:57:04

Message: 1 of 11

I have limited MATLAB experience and have spent a day trying to work this problem out so would be most appreciative of any help available here!

I have 600+ (potentially more in the future) ASCII files with data like this:

header 1
header 2
header 3 etc to 21
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
-1 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 (1000 rows)
header 1
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
-1 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0

This continues for 80 datasets each with 10,000 pieces of data in them.
I would love to be able to load the data in and separate each of the 'sets' rather than having to do this in ASCII first.

I was trying:

fid=fopen('filename.txt);

data1=textscan(fid, %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 'HeaderLines',21);

data2=textscan(fid, %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 'HeaderLines',1022);


fclose(fid);

I have tried numerous combinations of this to try and get it to work and I am definitely getting something wrong!

If you could help that would be fantastic.

Subject: Error using textscan and headerlines

From: Doug Schwarz

Date: 28 Mar, 2011 16:19:40

Message: 2 of 11

On 3/28/2011 11:57 AM, Katherine wrote:
> I have limited MATLAB experience and have spent a day trying to work
> this problem out so would be most appreciative of any help available here!
>
> I have 600+ (potentially more in the future) ASCII files with data like
> this:
>
> header 1
> header 2
> header 3 etc to 21
> 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
> -1 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 (1000 rows)
> header 1
> 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
> -1 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
>
> This continues for 80 datasets each with 10,000 pieces of data in them.
> I would love to be able to load the data in and separate each of the
> 'sets' rather than having to do this in ASCII first.
>
> I was trying:
>
> fid=fopen('filename.txt);
>
> data1=textscan(fid, %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32
> 'HeaderLines',21);
>
> data2=textscan(fid, %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32
> 'HeaderLines',1022);
>
>
> fclose(fid);
>
> I have tried numerous combinations of this to try and get it to work and
> I am definitely getting something wrong!
>
> If you could help that would be fantastic.


Something like

fid = fopen(...);
data = cell(1,80);
data{1} = textscan(fid,format,1000,'HeaderLines',21);
for i = 2:80
     data{i} = textscan(fid,format,1000,'HeaderLines',1);
end

Should do it. Remember, each textscan picks up in the file from where
the last one left off so you don't want to skip more than the new single
header line.

Each cell of data will contain the cell array returned by textscan so
don't be confused by the number of braces {} you'll need.

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

Subject: Error using textscan and headerlines

From: Katherine

Date: 29 Mar, 2011 09:44:03

Message: 3 of 11

"Katherine " <McCann-K7@email.ulster.ac.uk> wrote in message <imqb4g$ddg$1@fred.mathworks.com>...
> I have limited MATLAB experience and have spent a day trying to work this problem out so would be most appreciative of any help available here!
>
> I have 600+ (potentially more in the future) ASCII files with data like this:
>
> header 1
> header 2
> header 3 etc to 21
> 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
> -1 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 (1000 rows)
> header 1
> 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
> -1 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
>
> This continues for 80 datasets each with 10,000 pieces of data in them.
> I would love to be able to load the data in and separate each of the 'sets' rather than having to do this in ASCII first.
>
> I was trying:
>
> fid=fopen('filename.txt);
>
> data1=textscan(fid, %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 'HeaderLines',21);
>
> data2=textscan(fid, %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 'HeaderLines',1022);
>
>
> fclose(fid);
>
> I have tried numerous combinations of this to try and get it to work and I am definitely getting something wrong!
>
> If you could help that would be fantastic.

Thanks for that Doug. I am now getting an error message when I input the
%data{1}=textscan(fid,format,1000,'Headerlines',21)
Error message: 'Error using ==>format
Too many output arguments'

Can you advise?

Thanks

Katherine

Subject: Error using textscan and headerlines

From: Doug Schwarz

Date: 29 Mar, 2011 11:02:53

Message: 4 of 11

In article <ims9l3$7hg$1@fred.mathworks.com>,
 "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:

> Thanks for that Doug. I am now getting an error message when I input the
> %data{1}=textscan(fid,format,1000,'Headerlines',21)
> Error message: 'Error using ==>format
> Too many output arguments'
>
> Can you advise?
>
> Thanks
>
> Katherine

Yes, sorry I wasn't more explicit. I meant for you to place your format
string there. And I guess you shouldn't use 'format' because that's a
function name. Try

fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32';
fid = fopen(...);
data = cell(1,80);
data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
for i = 2:80
     data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
end

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

Subject: Error using textscan and headerlines

From: Katherine

Date: 5 Apr, 2011 14:02:21

Message: 5 of 11

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-F1549C.07025229032011@news.frontiernet.net>...
> In article <ims9l3$7hg$1@fred.mathworks.com>,
> "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:
>
> > Thanks for that Doug. I am now getting an error message when I input the
> > %data{1}=textscan(fid,format,1000,'Headerlines',21)
> > Error message: 'Error using ==>format
> > Too many output arguments'
> >
> > Can you advise?
> >
> > Thanks
> >
> > Katherine
>
> Yes, sorry I wasn't more explicit. I meant for you to place your format
> string there. And I guess you shouldn't use 'format' because that's a
> function name. Try
>
> fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32';
> fid = fopen(...);
> data = cell(1,80);
> data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
> for i = 2:80
> data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
> end
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Thank you for that Doug, I didn't receive an alert to say you had replied and had worked this out myself!
I am not receiving any error messages now but when I try to plot the data i.e.
plot(data{1}) it now says
??? error using ==> plot
Conversion to double from cell is not possible

If I am able to plot the data originally (before textscan etc) I don't understand why I am not able to after this process.

Hoping you can help! (again!)

Thanks

Katherine

Subject: Error using textscan and headerlines

From: Katherine

Date: 5 Apr, 2011 15:20:05

Message: 6 of 11

"Katherine " <McCann-K7@email.ulster.ac.uk> wrote in message <inf7dd$9tv$1@fred.mathworks.com>...
> Doug Schwarz <see@sig.for.address.edu> wrote in message <see-F1549C.07025229032011@news.frontiernet.net>...
> > In article <ims9l3$7hg$1@fred.mathworks.com>,
> > "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:
> >
> > > Thanks for that Doug. I am now getting an error message when I input the
> > > %data{1}=textscan(fid,format,1000,'Headerlines',21)
> > > Error message: 'Error using ==>format
> > > Too many output arguments'
> > >
> > > Can you advise?
> > >
> > > Thanks
> > >
> > > Katherine
> >
> > Yes, sorry I wasn't more explicit. I meant for you to place your format
> > string there. And I guess you shouldn't use 'format' because that's a
> > function name. Try
> >
> > fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32';
> > fid = fopen(...);
> > data = cell(1,80);
> > data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
> > for i = 2:80
> > data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
> > end
> >
> > --
> > Doug Schwarz
> > dmschwarz&ieee,org
> > Make obvious changes to get real email address.
>
> Thank you for that Doug, I didn't receive an alert to say you had replied and had worked this out myself!
> I am not receiving any error messages now but when I try to plot the data i.e.
> plot(data{1}) it now says
> ??? error using ==> plot
> Conversion to double from cell is not possible
>
> If I am able to plot the data originally (before textscan etc) I don't understand why I am not able to after this process.
>
> Hoping you can help! (again!)
>
> Thanks
>
> Katherine

Hi again Doug (please don't groan!)
I sorted the problem with plot function - just had to convery the array into a matrix again.
I am noticing that I am getting answers for data{1} but not with data{2} onwards when I use your suggestion.
It is just showing ans = [] when I try to have a look at the contents.
Any suggestions?

Thanks again

Katherine

Subject: Error using textscan and headerlines

From: Doug Schwarz

Date: 6 Apr, 2011 02:51:17

Message: 7 of 11

In article <infbv5$6ve$1@fred.mathworks.com>,
 "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:

> "Katherine " <McCann-K7@email.ulster.ac.uk> wrote in message
> <inf7dd$9tv$1@fred.mathworks.com>...
> > Doug Schwarz <see@sig.for.address.edu> wrote in message
> > <see-F1549C.07025229032011@news.frontiernet.net>...
> > > In article <ims9l3$7hg$1@fred.mathworks.com>,
> > > "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:
> > >
> > > > Thanks for that Doug. I am now getting an error message when I input
> > > > the
> > > > %data{1}=textscan(fid,format,1000,'Headerlines',21)
> > > > Error message: 'Error using ==>format
> > > > Too many output arguments'
> > > >
> > > > Can you advise?
> > > >
> > > > Thanks
> > > >
> > > > Katherine
> > >
> > > Yes, sorry I wasn't more explicit. I meant for you to place your format
> > > string there. And I guess you shouldn't use 'format' because that's a
> > > function name. Try
> > >
> > > fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32';
> > > fid = fopen(...);
> > > data = cell(1,80);
> > > data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
> > > for i = 2:80
> > > data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
> > > end
> > >
> > > --
> > > Doug Schwarz
> > > dmschwarz&ieee,org
> > > Make obvious changes to get real email address.
> >
> > Thank you for that Doug, I didn't receive an alert to say you had replied
> > and had worked this out myself!
> > I am not receiving any error messages now but when I try to plot the data
> > i.e.
> > plot(data{1}) it now says
> > ??? error using ==> plot
> > Conversion to double from cell is not possible
> >
> > If I am able to plot the data originally (before textscan etc) I don't
> > understand why I am not able to after this process.
> >
> > Hoping you can help! (again!)
> >
> > Thanks
> >
> > Katherine
>
> Hi again Doug (please don't groan!)
> I sorted the problem with plot function - just had to convery the array into
> a matrix again.
> I am noticing that I am getting answers for data{1} but not with data{2}
> onwards when I use your suggestion.
> It is just showing ans = [] when I try to have a look at the contents.
> Any suggestions?
>
> Thanks again
>
> Katherine

Katherine,

I forgot something. I will illustrate with a small example. Let's say
your file looks like this (not including the dashed lines):

---------------------------
header1
header2
header3
1 2 3
4 5 6
header4
7 8 9
10 11 12
---------------------------

And we try to load it with:

fmt = '%f%f%f';
fid = fopen('thefile.txt');
data = cell(1,2);
data{1} = textread(fid,fmt,2,'HeaderLines',3);

At this point the file has been read up to and including the '6', but
not including the newline (\n) that comes after it. Next we try

data{2} = textread(fid,fmt,2,'HeaderLines',1);

but textread sees the text '\nheader4\n7 8 9\n10 11 12\n' which looks
like *two* header lines before the real data so the format is not
satisfied and it reads nothing. You should be able to fix it by
including the newline in the format string *or* specifying one more
header line in the loop. So either

%---------------------
fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32\n';
fid = fopen(...);
data = cell(1,80);
data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
for i = 2:80
     data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
end
%---------------------

or

%---------------------
fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32';
fid = fopen(...);
data = cell(1,80);
data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
for i = 2:80
     data{i} = textscan(fid,fmt_str,1000,'HeaderLines',2);
end
%---------------------

should work.

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

Subject: Error using textscan and headerlines

From: Katherine

Date: 6 Apr, 2011 14:45:25

Message: 8 of 11

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-72F85E.22511705042011@news.frontiernet.net>...
> In article <infbv5$6ve$1@fred.mathworks.com>,
> "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:
>
> > "Katherine " <McCann-K7@email.ulster.ac.uk> wrote in message
> > <inf7dd$9tv$1@fred.mathworks.com>...
> > > Doug Schwarz <see@sig.for.address.edu> wrote in message
> > > <see-F1549C.07025229032011@news.frontiernet.net>...
> > > > In article <ims9l3$7hg$1@fred.mathworks.com>,
> > > > "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:
> > > >
> > > > > Thanks for that Doug. I am now getting an error message when I input
> > > > > the
> > > > > %data{1}=textscan(fid,format,1000,'Headerlines',21)
> > > > > Error message: 'Error using ==>format
> > > > > Too many output arguments'
> > > > >
> > > > > Can you advise?
> > > > >
> > > > > Thanks
> > > > >
> > > > > Katherine
> > > >
> > > > Yes, sorry I wasn't more explicit. I meant for you to place your format
> > > > string there. And I guess you shouldn't use 'format' because that's a
> > > > function name. Try
> > > >
> > > > fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32';
> > > > fid = fopen(...);
> > > > data = cell(1,80);
> > > > data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
> > > > for i = 2:80
> > > > data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
> > > > end
> > > >
> > > > --
> > > > Doug Schwarz
> > > > dmschwarz&ieee,org
> > > > Make obvious changes to get real email address.
> > >
> > > Thank you for that Doug, I didn't receive an alert to say you had replied
> > > and had worked this out myself!
> > > I am not receiving any error messages now but when I try to plot the data
> > > i.e.
> > > plot(data{1}) it now says
> > > ??? error using ==> plot
> > > Conversion to double from cell is not possible
> > >
> > > If I am able to plot the data originally (before textscan etc) I don't
> > > understand why I am not able to after this process.
> > >
> > > Hoping you can help! (again!)
> > >
> > > Thanks
> > >
> > > Katherine
> >
> > Hi again Doug (please don't groan!)
> > I sorted the problem with plot function - just had to convery the array into
> > a matrix again.
> > I am noticing that I am getting answers for data{1} but not with data{2}
> > onwards when I use your suggestion.
> > It is just showing ans = [] when I try to have a look at the contents.
> > Any suggestions?
> >
> > Thanks again
> >
> > Katherine
>
> Katherine,
>
> I forgot something. I will illustrate with a small example. Let's say
> your file looks like this (not including the dashed lines):
>
> ---------------------------
> header1
> header2
> header3
> 1 2 3
> 4 5 6
> header4
> 7 8 9
> 10 11 12
> ---------------------------
>
> And we try to load it with:
>
> fmt = '%f%f%f';
> fid = fopen('thefile.txt');
> data = cell(1,2);
> data{1} = textread(fid,fmt,2,'HeaderLines',3);
>
> At this point the file has been read up to and including the '6', but
> not including the newline (\n) that comes after it. Next we try
>
> data{2} = textread(fid,fmt,2,'HeaderLines',1);
>
> but textread sees the text '\nheader4\n7 8 9\n10 11 12\n' which looks
> like *two* header lines before the real data so the format is not
> satisfied and it reads nothing. You should be able to fix it by
> including the newline in the format string *or* specifying one more
> header line in the loop. So either
>
> %---------------------
> fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32\n';
> fid = fopen(...);
> data = cell(1,80);
> data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
> for i = 2:80
> data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
> end
> %---------------------
>
> or
>
> %---------------------
> fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32';
> fid = fopen(...);
> data = cell(1,80);
> data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
> for i = 2:80
> data{i} = textscan(fid,fmt_str,1000,'HeaderLines',2);
> end
> %---------------------
>
> should work.
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Hi Doug,

I was thinking that myself actually, about adding another number to the data{i} as thought the cursor might be left at the end of the last dataset and so would need to effectively skip 2 headers but alas it hasn't done the trick!
Is this symbol \n refer to a new line in matlab? I am getting very strange answers when I add it to the end of the fmt_str.
Also when I use the '2' rather than the one in the data{i} section I am still not getting any answers for the data{2} onwards. I will continue to look at this this evening.

Thanks again

Katherine

Subject: Error using textscan and headerlines

From: Doug Schwarz

Date: 6 Apr, 2011 15:46:04

Message: 9 of 11

On 4/6/2011 10:45 AM, Katherine wrote:
> Doug Schwarz <see@sig.for.address.edu> wrote in message
> <see-72F85E.22511705042011@news.frontiernet.net>...
>> In article <infbv5$6ve$1@fred.mathworks.com>,
>> "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:
>>
>> > "Katherine " <McCann-K7@email.ulster.ac.uk> wrote in message >
>> <inf7dd$9tv$1@fred.mathworks.com>...
>> > > Doug Schwarz <see@sig.for.address.edu> wrote in message > >
>> <see-F1549C.07025229032011@news.frontiernet.net>...
>> > > > In article <ims9l3$7hg$1@fred.mathworks.com>,
>> > > > "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:
>> > > > > > > > Thanks for that Doug. I am now getting an error message
>> when I input > > > > the > > > >
>> %data{1}=textscan(fid,format,1000,'Headerlines',21)
>> > > > > Error message: 'Error using ==>format
>> > > > > Too many output arguments'
>> > > > > > > > > Can you advise?
>> > > > > > > > > Thanks
>> > > > > > > > > Katherine
>> > > > > > > Yes, sorry I wasn't more explicit. I meant for you to
>> place your format > > > string there. And I guess you shouldn't use
>> 'format' because that's a > > > function name. Try
>> > > > > > > fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32
>> %f32';
>> > > > fid = fopen(...);
>> > > > data = cell(1,80);
>> > > > data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
>> > > > for i = 2:80
>> > > > data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
>> > > > end
>> > > > > > > -- > > > Doug Schwarz
>> > > > dmschwarz&ieee,org
>> > > > Make obvious changes to get real email address.
>> > > > > Thank you for that Doug, I didn't receive an alert to say you
>> had replied > > and had worked this out myself!
>> > > I am not receiving any error messages now but when I try to plot
>> the data > > i.e.
>> > > plot(data{1}) it now says > > ??? error using ==> plot
>> > > Conversion to double from cell is not possible
>> > > > > If I am able to plot the data originally (before textscan etc)
>> I don't > > understand why I am not able to after this process.
>> > > > > Hoping you can help! (again!)
>> > > > > Thanks
>> > > > > Katherine
>> > > Hi again Doug (please don't groan!)
>> > I sorted the problem with plot function - just had to convery the
>> array into > a matrix again.
>> > I am noticing that I am getting answers for data{1} but not with
>> data{2} > onwards when I use your suggestion.
>> > It is just showing ans = [] when I try to have a look at the contents.
>> > Any suggestions?
>> > > Thanks again
>> > > Katherine
>>
>> Katherine,
>>
>> I forgot something. I will illustrate with a small example. Let's say
>> your file looks like this (not including the dashed lines):
>>
>> ---------------------------
>> header1
>> header2
>> header3
>> 1 2 3
>> 4 5 6
>> header4
>> 7 8 9
>> 10 11 12
>> ---------------------------
>>
>> And we try to load it with:
>>
>> fmt = '%f%f%f';
>> fid = fopen('thefile.txt');
>> data = cell(1,2);
>> data{1} = textread(fid,fmt,2,'HeaderLines',3);
>>
>> At this point the file has been read up to and including the '6', but
>> not including the newline (\n) that comes after it. Next we try
>>
>> data{2} = textread(fid,fmt,2,'HeaderLines',1);
>>
>> but textread sees the text '\nheader4\n7 8 9\n10 11 12\n' which looks
>> like *two* header lines before the real data so the format is not
>> satisfied and it reads nothing. You should be able to fix it by
>> including the newline in the format string *or* specifying one more
>> header line in the loop. So either
>>
>> %---------------------
>> fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32\n';
>> fid = fopen(...);
>> data = cell(1,80);
>> data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
>> for i = 2:80
>> data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
>> end
>> %---------------------
>>
>> or
>>
>> %---------------------
>> fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32';
>> fid = fopen(...);
>> data = cell(1,80);
>> data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
>> for i = 2:80
>> data{i} = textscan(fid,fmt_str,1000,'HeaderLines',2);
>> end
>> %---------------------
>>
>> should work.
>>
>> --
>> Doug Schwarz
>> dmschwarz&ieee,org
>> Make obvious changes to get real email address.
>
> Hi Doug,
>
> I was thinking that myself actually, about adding another number to the
> data{i} as thought the cursor might be left at the end of the last
> dataset and so would need to effectively skip 2 headers but alas it
> hasn't done the trick!
> Is this symbol \n refer to a new line in matlab? I am getting very
> strange answers when I add it to the end of the fmt_str.
> Also when I use the '2' rather than the one in the data{i} section I am
> still not getting any answers for the data{2} onwards. I will continue
> to look at this this evening.
>
> Thanks again
>
> Katherine


Katherine,

Okay, one more detail. You didn't say what platform you're using, but
since the above didn't work I think you must be on Windows. On Windows,
you have to open the file in text mode. I'm pretty sure this will work
for you:

fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32\n';
fid = fopen('filename.txt','rt');
data = cell(1,80);
data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
for i = 2:80
      data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
end

If not, there might be some other invisible characters in there.

Yes, the '\n' is a newline which in Windows is really the two character
end-of-line sequence, (13,10) (carriage return plus newline). Opening
the file in text mode (the 't' in the second argument of fopen -- the
'r' means to open the file for reading) tells MATLAB to translate
appropriately. Text mode is not needed on other platforms, but will do
no harm so it's always okay to use it.


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

Subject: Error using textscan and headerlines

From: Florin Neacsu

Date: 6 Apr, 2011 16:32:20

Message: 10 of 11

"Katherine " <McCann-K7@email.ulster.ac.uk> wrote in message <imqb4g$ddg$1@fred.mathworks.com>...
> I have limited MATLAB experience and have spent a day trying to work this problem out so would be most appreciative of any help available here!
>
> I have 600+ (potentially more in the future) ASCII files with data like this:
>
> header 1
> header 2
> header 3 etc to 21
> 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
> -1 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 (1000 rows)
> header 1
> 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
> -1 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
>
> This continues for 80 datasets each with 10,000 pieces of data in them.
> I would love to be able to load the data in and separate each of the 'sets' rather than having to do this in ASCII first.
>
> I was trying:
>
> fid=fopen('filename.txt);
>
> data1=textscan(fid, %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 'HeaderLines',21);
>
> data2=textscan(fid, %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 'HeaderLines',1022);
>
>
> fclose(fid);
>
> I have tried numerous combinations of this to try and get it to work and I am definitely getting something wrong!
>
> If you could help that would be fantastic.

Hello,

I don't know if you have solved this, but this is an (possibly slow) alternative

----------------------------------------------
fid = fopen('foo.txt','r');
temp=zeros(1,10);
while 1
    tline = fgetl(fid);
    if ~ischar(tline), break, end
    x=strfind(tline,'header');
    if ~isempty(x)
        continue
    else
        i=1;
        while true
        [str, tline] = strtok(tline);
        if isempty(str), break; end
        temp(i)=str2num(str);
        i=i+1;
        end
    end
    temp
end
fclose(fid);
--------------------------------------------------------

temp will contain the 10 numbers on your line. I didn't know how you wanted to store these lines, so I'll leave that to you. Also, temp=zeros(1,10) because I saw you had 10 on each line.

Hope this helps.

Florin

Subject: Error using textscan and headerlines

From: Katherine

Date: 7 Apr, 2011 12:20:20

Message: 11 of 11

Doug Schwarz <see@sig.for.address.edu> wrote in message <2V%mp.14326$sP1.13859@newsfe07.iad>...
> On 4/6/2011 10:45 AM, Katherine wrote:
> > Doug Schwarz <see@sig.for.address.edu> wrote in message
> > <see-72F85E.22511705042011@news.frontiernet.net>...
> >> In article <infbv5$6ve$1@fred.mathworks.com>,
> >> "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:
> >>
> >> > "Katherine " <McCann-K7@email.ulster.ac.uk> wrote in message >
> >> <inf7dd$9tv$1@fred.mathworks.com>...
> >> > > Doug Schwarz <see@sig.for.address.edu> wrote in message > >
> >> <see-F1549C.07025229032011@news.frontiernet.net>...
> >> > > > In article <ims9l3$7hg$1@fred.mathworks.com>,
> >> > > > "Katherine " <McCann-K7@email.ulster.ac.uk> wrote:
> >> > > > > > > > Thanks for that Doug. I am now getting an error message
> >> when I input > > > > the > > > >
> >> %data{1}=textscan(fid,format,1000,'Headerlines',21)
> >> > > > > Error message: 'Error using ==>format
> >> > > > > Too many output arguments'
> >> > > > > > > > > Can you advise?
> >> > > > > > > > > Thanks
> >> > > > > > > > > Katherine
> >> > > > > > > Yes, sorry I wasn't more explicit. I meant for you to
> >> place your format > > > string there. And I guess you shouldn't use
> >> 'format' because that's a > > > function name. Try
> >> > > > > > > fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32
> >> %f32';
> >> > > > fid = fopen(...);
> >> > > > data = cell(1,80);
> >> > > > data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
> >> > > > for i = 2:80
> >> > > > data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
> >> > > > end
> >> > > > > > > -- > > > Doug Schwarz
> >> > > > dmschwarz&ieee,org
> >> > > > Make obvious changes to get real email address.
> >> > > > > Thank you for that Doug, I didn't receive an alert to say you
> >> had replied > > and had worked this out myself!
> >> > > I am not receiving any error messages now but when I try to plot
> >> the data > > i.e.
> >> > > plot(data{1}) it now says > > ??? error using ==> plot
> >> > > Conversion to double from cell is not possible
> >> > > > > If I am able to plot the data originally (before textscan etc)
> >> I don't > > understand why I am not able to after this process.
> >> > > > > Hoping you can help! (again!)
> >> > > > > Thanks
> >> > > > > Katherine
> >> > > Hi again Doug (please don't groan!)
> >> > I sorted the problem with plot function - just had to convery the
> >> array into > a matrix again.
> >> > I am noticing that I am getting answers for data{1} but not with
> >> data{2} > onwards when I use your suggestion.
> >> > It is just showing ans = [] when I try to have a look at the contents.
> >> > Any suggestions?
> >> > > Thanks again
> >> > > Katherine
> >>
> >> Katherine,
> >>
> >> I forgot something. I will illustrate with a small example. Let's say
> >> your file looks like this (not including the dashed lines):
> >>
> >> ---------------------------
> >> header1
> >> header2
> >> header3
> >> 1 2 3
> >> 4 5 6
> >> header4
> >> 7 8 9
> >> 10 11 12
> >> ---------------------------
> >>
> >> And we try to load it with:
> >>
> >> fmt = '%f%f%f';
> >> fid = fopen('thefile.txt');
> >> data = cell(1,2);
> >> data{1} = textread(fid,fmt,2,'HeaderLines',3);
> >>
> >> At this point the file has been read up to and including the '6', but
> >> not including the newline (\n) that comes after it. Next we try
> >>
> >> data{2} = textread(fid,fmt,2,'HeaderLines',1);
> >>
> >> but textread sees the text '\nheader4\n7 8 9\n10 11 12\n' which looks
> >> like *two* header lines before the real data so the format is not
> >> satisfied and it reads nothing. You should be able to fix it by
> >> including the newline in the format string *or* specifying one more
> >> header line in the loop. So either
> >>
> >> %---------------------
> >> fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32\n';
> >> fid = fopen(...);
> >> data = cell(1,80);
> >> data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
> >> for i = 2:80
> >> data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
> >> end
> >> %---------------------
> >>
> >> or
> >>
> >> %---------------------
> >> fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32';
> >> fid = fopen(...);
> >> data = cell(1,80);
> >> data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
> >> for i = 2:80
> >> data{i} = textscan(fid,fmt_str,1000,'HeaderLines',2);
> >> end
> >> %---------------------
> >>
> >> should work.
> >>
> >> --
> >> Doug Schwarz
> >> dmschwarz&ieee,org
> >> Make obvious changes to get real email address.
> >
> > Hi Doug,
> >
> > I was thinking that myself actually, about adding another number to the
> > data{i} as thought the cursor might be left at the end of the last
> > dataset and so would need to effectively skip 2 headers but alas it
> > hasn't done the trick!
> > Is this symbol \n refer to a new line in matlab? I am getting very
> > strange answers when I add it to the end of the fmt_str.
> > Also when I use the '2' rather than the one in the data{i} section I am
> > still not getting any answers for the data{2} onwards. I will continue
> > to look at this this evening.
> >
> > Thanks again
> >
> > Katherine
>
>
> Katherine,
>
> Okay, one more detail. You didn't say what platform you're using, but
> since the above didn't work I think you must be on Windows. On Windows,
> you have to open the file in text mode. I'm pretty sure this will work
> for you:
>
> fmt_str = '%f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32 %f32\n';
> fid = fopen('filename.txt','rt');
> data = cell(1,80);
> data{1} = textscan(fid,fmt_str,1000,'HeaderLines',21);
> for i = 2:80
> data{i} = textscan(fid,fmt_str,1000,'HeaderLines',1);
> end
>
> If not, there might be some other invisible characters in there.
>
> Yes, the '\n' is a newline which in Windows is really the two character
> end-of-line sequence, (13,10) (carriage return plus newline). Opening
> the file in text mode (the 't' in the second argument of fopen -- the
> 'r' means to open the file for reading) tells MATLAB to translate
> appropriately. Text mode is not needed on other platforms, but will do
> no harm so it's always okay to use it.
>
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Thanks for this Doug!! It is now working perfectly - the best thing is I can follow it!
As a newbie I find the 'help' documentation can be very wordy.
Thanks again

Katherine

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