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:
reading a data file to searches on Date

Subject: reading a data file to searches on Date

From: John Smith

Date: 4 Oct, 2008 13:27:01

Message: 1 of 6

Hi,
 I wanted to read in a file with the following format

Date number number

where date is a string such as "4-Oct-08" and numbers are floating point.

 I want to be able to do some searches (using find) on the Date. How do I read in the file such that I can do the searches on the date ?

 Many thanks.

John.

Subject: reading a data file to searches on Date

From: Andres

Date: 4 Oct, 2008 19:40:04

Message: 2 of 6

"John Smith" <cc200802@yahoo.com> wrote in message <gc7qv5$pv0$1@fred.mathworks.com>...
> Hi,
> I wanted to read in a file with the following format
>
> Date number number
>
> where date is a string such as "4-Oct-08" and numbers are floating point.
>
> I want to be able to do some searches (using find) on the Date. How do I read in the file such that I can do the searches on the date ?

Hi John,
use textscan to read in the data, i.e. something like

fname = 'c:\jsmith.txt';
fid = fopen(fname);
A = textscan(fid, '%s %f %f');
fclose(fid);

and if necessary apply one of the date functions (like datevec) to the first element of A (which stores the date strings in a cell array of strings). The respective doc pages will give you more insight into this.
I hope this gets you started.
Regards
Andres

Subject: reading a data file to searches on Date

From: John Smith

Date: 5 Oct, 2008 14:28:01

Message: 3 of 6

Hi Andres,
Thank you very much.

John

"Andres " <rantore@werb.deNoRs> wrote in message <gc8gqk$49v$1@fred.mathworks.com>...
> "John Smith" <cc200802@yahoo.com> wrote in message <gc7qv5$pv0$1@fred.mathworks.com>...
> > Hi,
> > I wanted to read in a file with the following format
> >
> > Date number number
> >
> > where date is a string such as "4-Oct-08" and numbers are floating point.
> >
> > I want to be able to do some searches (using find) on the Date. How do I read in the file such that I can do the searches on the date ?
>
> Hi John,
> use textscan to read in the data, i.e. something like
>
> fname = 'c:\jsmith.txt';
> fid = fopen(fname);
> A = textscan(fid, '%s %f %f');
> fclose(fid);
>
> and if necessary apply one of the date functions (like datevec) to the first element of A (which stores the date strings in a cell array of strings). The respective doc pages will give you more insight into this.
> I hope this gets you started.
> Regards
> Andres

Subject: reading a data file to searches on Date

From: John Smith

Date: 5 Oct, 2008 15:05:02

Message: 4 of 6

Thanks again for your help. I'm still strugling to achieve what I want. I'm not able to read the data and when I can see that I've read all the data into cell arrays.

for example, i see

when I print A, where A=textscan(fid, '%s %f %f');
    {1202x1 cell} [1202x1 double] [1202x1 double]

I now want to do something like
IDX = find(A{1} == '05-Sep-08')
it gives me error,


??? Undefined function or method 'eq' for input arguments of type 'cell'.

so I tried
IDX = find(datevec(A{1} == datevec(05-Sep-08'))

??? Error using ==> eq
Matrix dimensions must agree.

 
appreciate any help

Thank you.

John

"Andres " <rantore@werb.deNoRs> wrote in message <gc8gqk$49v$1@fred.mathworks.com>...
> "John Smith" <cc200802@yahoo.com> wrote in message <gc7qv5$pv0$1@fred.mathworks.com>...
> > Hi,
> > I wanted to read in a file with the following format
> >
> > Date number number
> >
> > where date is a string such as "4-Oct-08" and numbers are floating point.
> >
> > I want to be able to do some searches (using find) on the Date. How do I read in the file such that I can do the searches on the date ?
>
> Hi John,
> use textscan to read in the data, i.e. something like
>
> fname = 'c:\jsmith.txt';
> fid = fopen(fname);
> A = textscan(fid, '%s %f %f');
> fclose(fid);
>
> and if necessary apply one of the date functions (like datevec) to the first element of A (which stores the date strings in a cell array of strings). The respective doc pages will give you more insight into this.
> I hope this gets you started.
> Regards
> Andres

Subject: reading a data file to searches on Date

From: Andres

Date: 5 Oct, 2008 16:30:17

Message: 5 of 6

"John Smith" <cc200802@yahoo.com> wrote in message <gcal2u$aci$1@fred.mathworks.com>...
> [..]
> for example, i see
>
> when I print A, where A=textscan(fid, '%s %f %f');
> {1202x1 cell} [1202x1 double] [1202x1 double]
>
> I now want to do something like
> IDX = find(A{1} == '05-Sep-08')
> it gives me error,
>
>
> ??? Undefined function or method 'eq' for input arguments of type 'cell'.
>
> so I tried
> IDX = find(datevec(A{1} == datevec(05-Sep-08'))
>
> ??? Error using ==> eq
> Matrix dimensions must agree.
>
>
> [..]

Hi John,
don't use "==" a) on strings or b) to compare whole rows, it's meant as a numeric element-by-element operation (type doc eq). Use a string function instead, like strmatch or its relatives - see the doc.
Hth
Andres

Subject: reading a data file to searches on Date

From: John Smith

Date: 6 Oct, 2008 18:40:22

Message: 6 of 6

Hi Andres,
 Thank you very much.

John

"Andres " <rantore@werb.deNoRs> wrote in message <gcaq2p$7lv$1@fred.mathworks.com>...
> "John Smith" <cc200802@yahoo.com> wrote in message <gcal2u$aci$1@fred.mathworks.com>...
> > [..]
> > for example, i see
> >
> > when I print A, where A=textscan(fid, '%s %f %f');
> > {1202x1 cell} [1202x1 double] [1202x1 double]
> >
> > I now want to do something like
> > IDX = find(A{1} == '05-Sep-08')
> > it gives me error,
> >
> >
> > ??? Undefined function or method 'eq' for input arguments of type 'cell'.
> >
> > so I tried
> > IDX = find(datevec(A{1} == datevec(05-Sep-08'))
> >
> > ??? Error using ==> eq
> > Matrix dimensions must agree.
> >
> >
> > [..]
>
> Hi John,
> don't use "==" a) on strings or b) to compare whole rows, it's meant as a numeric element-by-element operation (type doc eq). Use a string function instead, like strmatch or its relatives - see the doc.
> Hth
> Andres

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