Thread Subject:
changing time and date from string to number

Subject: changing time and date from string to number

From: Matt

Date: 28 Jun, 2012 13:59:08

Message: 1 of 9

Hey all,

I'm stuck on a transfer that I'm working on. I'm extracting a time and date from the name of a file (e.g. sound_11_30_00__5_4_12.wav where 11_30_00 is 11:30 am and 5_4_12 is 5 April 2012) and attempting to transfer it to a number to look like 11:30 and 5 April 2012. Problem I'm running into is that its in a for loop and they are being saved as variables so str2num isn't working like it would outside of the loop i.e. I can't just tell str2num what string to operate on since they are variables. I haven't seen any ways to do this from looking around so I'm hoping someone here could at least point me in the right direction. I'm a novice at MATLAB so perhaps there is another function to use besides str2num first? Thanks!

Subject: changing time and date from string to number

From: dpb

Date: 28 Jun, 2012 17:48:49

Message: 2 of 9

On 6/28/2012 8:59 AM, Matt wrote:
> Hey all,
>
> I'm stuck on a transfer that I'm working on. I'm extracting a time and
> date from the name of a file (e.g. sound_11_30_00__5_4_12.wav where
> 11_30_00 is 11:30 am and 5_4_12 is 5 April 2012) and attempting to
> transfer it to a number to look like 11:30 and 5 April 2012. Problem I'm
> running into is that its in a for loop and they are being saved as
> variables so str2num isn't working like it would outside of the loop
> i.e. I can't just tell str2num what string to operate on since they are
> variables. I haven't seen any ways to do this from looking around so I'm
> hoping someone here could at least point me in the right direction. I'm
> a novice at MATLAB so perhaps there is another function to use besides
> str2num first? Thanks!

 >> s='sound_11_30_00__5_4_12.wav';
 >> [hr,mn,sc,da,mo,yr]=strread(s,'sound_%d_%d_%d__%d_%d_%d.wav');
 >> datestr(datenum(yr,mo,da,hr,mn,sc))
ans =
05-Apr-0012 11:30:00
 >>

The above can work on vectors as inputs...

--

Subject: changing time and date from string to number

From: dpb

Date: 28 Jun, 2012 19:59:46

Message: 3 of 9

On 6/28/2012 12:48 PM, dpb wrote:
...

> >> s='sound_11_30_00__5_4_12.wav';
> >> [hr,mn,sc,da,mo,yr]=strread(s,'sound_%d_%d_%d__%d_%d_%d.wav');
> >> datestr(datenum(yr,mo,da,hr,mn,sc))
> ans =
> 05-Apr-0012 11:30:00
> >>
>
> The above can work on vectors as inputs...
...

Forgot about the 2-digit year--just add the base century--assuming it's
current,

 >> datestr(datenum(2000+yr,mo,da,hr,mn,sc))
ans =
05-Apr-2012 11:30:00
 >>

--

Subject: changing time and date from string to number

From: Matt

Date: 28 Jun, 2012 20:35:09

Message: 4 of 9

dpb <none@non.net> wrote in message <jsid3i$i2u$1@speranza.aioe.org>...
> On 6/28/2012 12:48 PM, dpb wrote:
> ...
>
> > >> s='sound_11_30_00__5_4_12.wav';
> > >> [hr,mn,sc,da,mo,yr]=strread(s,'sound_%d_%d_%d__%d_%d_%d.wav');
> > >> datestr(datenum(yr,mo,da,hr,mn,sc))
> > ans =
> > 05-Apr-0012 11:30:00
> > >>
> >
> > The above can work on vectors as inputs...
> ...
>
> Forgot about the 2-digit year--just add the base century--assuming it's
> current,
>
> >> datestr(datenum(2000+yr,mo,da,hr,mn,sc))
> ans =
> 05-Apr-2012 11:30:00
> >>
>
> --
Thanks. That works perfect for one file, but how would I go about looping it since each filename would need to be printed out with strread command? I have hundreds of files following this same pattern, but strread needs the whole file entered each time into the 'format' section of the command so how could I loop it to do it for each individual file?

Subject: changing time and date from string to number

From: dpb

Date: 28 Jun, 2012 20:41:29

Message: 5 of 9

On 6/28/2012 3:35 PM, Matt wrote:
...

> Thanks. That works perfect for one file, but how would I go about
> looping it since each filename would need to be printed out with strread
> command? I have hundreds of files following this same pattern, but
> strread needs the whole file entered each time into the 'format' section
> of the command so how could I loop it to do it for each individual file?

Try again; I don't follow the question/see the problem well enough to
have an answer.

Are you trying to do something w/ a group of files and trying to
sequentially open them? If so, use dir() w/ an appropriate wild card
and process the result of the .name field...

d=dir('sound*.wav'); % maybe a suitable search???
for idx=1:length(d)
   fn=d(idx).name; % file name one at a time...
   % do whatever here
end

--

Subject: changing time and date from string to number

From: Matt

Date: 28 Jun, 2012 23:03:13

Message: 6 of 9


> Try again; I don't follow the question/see the problem well enough to
> have an answer.
>
> Are you trying to do something w/ a group of files and trying to
> sequentially open them? If so, use dir() w/ an appropriate wild card
> and process the result of the .name field...
>
> d=dir('sound*.wav'); % maybe a suitable search???
> for idx=1:length(d)
> fn=d(idx).name; % file name one at a time...
> % do whatever here
> end
>
> --

Thanks for the feedback. I really do appreciate it. I need to open 200 files from a folder, all with the basic filename "sound_hr_min_sec__day_mo_yr.wav". I'm currently reading them in one at a time and attempting to extract the time and date from each file. From what I understand about strread, I need to put in the command

>>s="1.sound_hr_min_sec__day_mo_yr.wav";
>>[hr min sec day mo yr]=strread(s,'1.sound_%d_%d_%d__%d_%d_%d.wav');

with the whole filename as the formatter for the strread command and then convert this to date and time stamp.

However, since I am looping each one in, what is the best way for MATLAB to print the filename as I've written above so it loops from 1-200 with the time and date changing everytime, as well as the first number in the filename changing? Could I just enter it like that and instead of the 1 in the formatter, put 'i' for my index? Or would that be read as part of the string itself, and if that's the case, is there an easy way to do it, or am I forced to enter each one manually? Thanks again, you've been awesome help!

Subject: changing time and date from string to number

From: dpb

Date: 28 Jun, 2012 23:55:55

Message: 7 of 9

On 6/28/2012 6:03 PM, Matt wrote:
>
>> Try again; I don't follow the question/see the problem well enough to
>> have an answer.
...

> Thanks for the feedback. I really do appreciate it. I need to open 200
> files from a folder, all with the basic filename
> "sound_hr_min_sec__day_mo_yr.wav". I'm currently reading them in one at
> a time and attempting to extract the time and date from each file. From
> what I understand about strread, I need to put in the command
>>> s="1.sound_hr_min_sec__day_mo_yr.wav";
>>> [hr min sec day mo yr]=strread(s,'1.sound_%d_%d_%d__%d_%d_%d.wav');
>
> with the whole filename as the formatter for the strread command and
> then convert this to date and time stamp.
...

No, you're mixing up the formatting string and the variable/string to
convert it appears...

d=dir('sound*.wav'); % a suitable search pattern
fmt='sound_%d_%d_%d__%d_%d_%d.wav'; % the conversion format string
for idx=1:length(d)
   [hr,mn,sc,da,mo,yr]=strread(d(idx).name,fmt);
   daytimestr=datestr(datenum(yr,mo,da,hr,mn,sc));
   % do whatever with the result string...
end

The only place I see you may have an issue will be if the format changes
between the time/date section. The example you've shown has two
underscores separating them instead of one but the day is a single
digit. If the second underscore is replaced by a digit when the day is
 >=10 then you'll have to fixup the format string to compensate by
testing for the existence of the '__' substring first. Easy enough,
just a detail to be handled if so. If you're making the files and can
specify the format, I'd suggest adding the leading 0 when writing the
filenames to make it consistent throughout. Plus, it will make any
sorting work "more better" by the ordering in sequence of days instead
of in alphabetical order.

--

Subject: changing time and date from string to number

From: Matt

Date: 29 Jun, 2012 00:20:23

Message: 8 of 9

>
> No, you're mixing up the formatting string and the variable/string to
> convert it appears...
>
> d=dir('sound*.wav'); % a suitable search pattern
> fmt='sound_%d_%d_%d__%d_%d_%d.wav'; % the conversion format string
> for idx=1:length(d)
> [hr,mn,sc,da,mo,yr]=strread(d(idx).name,fmt);
> daytimestr=datestr(datenum(yr,mo,da,hr,mn,sc));
> % do whatever with the result string...
> end
>
> The only place I see you may have an issue will be if the format changes
> between the time/date section. The example you've shown has two
> underscores separating them instead of one but the day is a single
> digit. If the second underscore is replaced by a digit when the day is
> >=10 then you'll have to fixup the format string to compensate by
> testing for the existence of the '__' substring first. Easy enough,
> just a detail to be handled if so. If you're making the files and can
> specify the format, I'd suggest adding the leading 0 when writing the
> filenames to make it consistent throughout. Plus, it will make any
> sorting work "more better" by the ordering in sequence of days instead
> of in alphabetical order.
>
> --


Awesome! Thank you so much! That's just what I needed.

Subject: changing time and date from string to number

From: dpb

Date: 29 Jun, 2012 14:36:48

Message: 9 of 9

On 6/28/2012 7:20 PM, Matt wrote:
>>
...

>> d=dir('sound*.wav'); % a suitable search pattern
>> fmt='sound_%d_%d_%d__%d_%d_%d.wav'; % the conversion format string
>> for idx=1:length(d)
>> [hr,mn,sc,da,mo,yr]=strread(d(idx).name,fmt);
>> daytimestr=datestr(datenum(yr,mo,da,hr,mn,sc));
>> % do whatever with the result string...
>> end
...

> Awesome! Thank you so much! That's just what I needed.

Don't forget to patch up the 2-digit year w/ the base century...which I
forgot again above...

daytimestr=datestr(datenum(1000+yr,mo,da,hr,mn,sc));

BTW, you're consistently chopping attributions (_not_a_good_thing_) as
well as snipping text to context (_a_good_thing_). Makes for keeping
track of who said what difficult; particularly if a thread gets more
than just the two participants.

--

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
time Matt 28 Jun, 2012 09:59:10
date Matt 28 Jun, 2012 09:59:10
str2num Matt 28 Jun, 2012 09:59:09
rssFeed for this Thread

Contact us