From: Arild on
I'm trying to read a .txt file on the format:
01.01.2005 00:00 23.6 25.7 etc.

So I'm using
fid = fopen('filename.txt','r')
C = textscan(fid, '%s %f %f')
numbers = [C{2}' C{3}'];
date = C{1}';
fclose(fid);

Now I want to extract the day, month and year, from the string "date", so I use

[day, month, year, remain] = strread(date, '%u %u %u %s','delimiter','.'); (1)

but this won't work, I just get
??? Error using ==> dataread
Second input must be a filename or string to parse

Isn't "date" an array of strings? I know the code (1) works when i define a string, like
teststring = '02.03.2009 00:00' and use it on the teststring.
Any suggestions? All the columns with numbers are working fine, it's the first column with dates I'm having trouble extracting information from.
From: Walter Roberson on
Arild wrote:
> I'm trying to read a .txt file on the format:
> 01.01.2005 00:00 23.6 25.7 etc.
>
> So I'm using
> fid = fopen('filename.txt','r')
> C = textscan(fid, '%s %f %f')

When you use %s and no Delimiter parameter, the string ends at the first
blank, so you the first use of the format would parse 01.01.2005 as a
string, then parse 00 as a floating point number, then probably choke on
the : and end there but if it keeps going it would parse the second 00
as %f, then because there is more data would go on and parse 23.6 as a
string, then 25.7 as %f and then would parse the next thing (not shown)
as %f, and so on.

> [day, month, year, remain] = strread(date, '%u %u %u
%s','delimiter','.'); (1)

Use textscan() instead of strred() and keep in mind that date is the
name of a built-in function.
From: Oleg Komarov on
"Arild " <arildhoy(a)stud.ntnu.no> wrote in message <hvpr5j$mot$1(a)fred.mathworks.com>...
> I'm trying to read a .txt file on the format:
> 01.01.2005 00:00 23.6 25.7 etc.
>
> So I'm using
> fid = fopen('filename.txt','r')
> C = textscan(fid, '%s %f %f')
> numbers = [C{2}' C{3}'];
> date = C{1}';
> fclose(fid);
>
> Now I want to extract the day, month and year, from the string "date", so I use
>
> [day, month, year, remain] = strread(date, '%u %u %u %s','delimiter','.'); (1)
>
> but this won't work, I just get
> ??? Error using ==> dataread
> Second input must be a filename or string to parse
>
> Isn't "date" an array of strings? I know the code (1) works when i define a string, like
> teststring = '02.03.2009 00:00' and use it on the teststring.
> Any suggestions? All the columns with numbers are working fine, it's the first column with dates I'm having trouble extracting information from.

Use datevec
Also I think date is cellstring meaning that date(1) is cell and date{1} is string.

Oleg
From: Arild on
Walter Roberson <roberson(a)hushmail.com> wrote in message <Yt_Tn.2423$Yo5.1140(a)newsfe01.iad>...
> Arild wrote:
> > I'm trying to read a .txt file on the format:
> > 01.01.2005 00:00 23.6 25.7 etc.
> >
> > So I'm using
> > fid = fopen('filename.txt','r')
> > C = textscan(fid, '%s %f %f')
>
> When you use %s and no Delimiter parameter, the string ends at the first
> blank, so you the first use of the format would parse 01.01.2005 as a
> string, then parse 00 as a floating point number, then probably choke on
> the : and end there but if it keeps going it would parse the second 00
> as %f, then because there is more data would go on and parse 23.6 as a
> string, then 25.7 as %f and then would parse the next thing (not shown)
> as %f, and so on.

You are right, I shortend the code a lot, there is a tabulator delimiter in the 'filename.txt', so when using this code, C{1} will contain
'01.01.2005 00:00' (a long vector with dates like this), and I am interested in extracting the date, month and year.

> > [day, month, year, remain] = strread(date, '%u %u %u
> %s','delimiter','.'); (1)
>
> Use textscan() instead of strred() and keep in mind that date is the
> name of a built-in function.

If I use
[day, month, year, remain] = textscan(date, '%f %f %f %s','delimiter','.');
I get error:
First input must be of type double or string.
I imagine it might be that the date/month/year are delimited by a '.', while there is just a whitespace between the year and '00:00'?
From: Walter Roberson on
Arild wrote:

>> > 01.01.2005 00:00 23.6 25.7 etc.
>> > > So I'm using
>> > fid = fopen('filename.txt','r')
>> > C = textscan(fid, '%s %f %f')

> If I use
> [day, month, year, remain] = textscan(date, '%f %f %f
> %s','delimiter','.');
> I get error:
> First input must be of type double or string.
> I imagine it might be that the date/month/year are delimited by a '.',
> while there is just a whitespace between the year and '00:00'?

You can use multiple delimiters, '.: ' or you can use width limitations
while reading:

'%2u.%2u.%u %u:%u %f %f....'

I have used a mix of fixed-width fields and indefinite width fields so
as to properly absorb the spaces. The '.' and ':' in the format will be
required to match literal '.' and ':' in the string.
 |  Next  |  Last
Pages: 1 2
Prev: Cleaning up Text
Next: FFT analysis