Bob's pick this week has a twist.
Contents
The challenge
Last week Brett's pick was a MATLAB alarm clock. But an alarm is only as good as the clock behind it. Your mission, should you accept, is to hook up the alarm to an atomic clock!
The pick
"How?" you might ask. That's where this week's pick comes in. Java Time by Scott Burnside is your other building block.
The catch
Scott's motivation was a Java example, so his MATLAB GUI does the deed using Java code and data types.
I suggest excising the portion of his code that fetches the time. Build a wrapper function to return that time value as a datenum for example.
The incentive
Submit your atomic alarm clock to the file exchange and it might just become our next "Pick of the Week." Good luck!
Talk to us
If you have questions or comments about this challenge, tell us about it here.
Get
the MATLAB code
Published with MATLAB® 7.6



How about this?
URL = ‘http://tycho.usno.navy.mil/cgi-bin/timer.pl’;
time = datenum(regexp(urlread(URL), ‘(.*)\sUTC’,'tokens’,'once’),’mmm. dd, HH:MM:SS’)
JesseL, thanks. That’s very close but I got an error.
??? Error using ==> datenum at 174 DATENUM failed. Caused by: Error using ==> dtstr2dtnummx Failed on converting date string to date number.Let’s see who fixes that…
That’s because it didn’t paste the right thing! I think the webpage is rendering the tag in the regular expression. The regexp string should be:
‘[BR](.*)\sUTC’
You should replace the [ ] with
Yup, like I thought, its rendering them. Because, the lessthan and greaterthan signs are gone from my previous comment.
Replace [ with lessthansign
Replace ] with greaterthansign
Maybe there’s an escape character?
Test: \
Yup, I think you’re right, JesseL. I had to use the character entities to display < and > correctly:
The following should be the correct code that you want:
P.S. BTW, there’s a “Preview” button just above the comment box, which you can use to preview your comment. This way, you can check to make sure the rendering is correct.
JesseL, that worked great.
>> URL = 'http://tycho.usno.navy.mil/cgi-bin/timer.pl'; >> time = datenum(regexp(urlread(URL),... '<BR>(.*)\sUTC','tokens','once'),'mmm. dd, HH:MM:SS') time = 7.3359e+005 >> datestr(time) ans = 02-Jul-2008 12:08:18Jiro, good formatting tips. Here’s a reminder that for anyone who wants to post code snippets be sure to use the <pre> tag around your code like this.
Then if your code includes some kind of <TAG> just replace it with <TAG> when you paste it as a blog comment so it will display correctly, and of course use Preview to make sure. (I had to do that several times.:)
Okay, so the first part of the challenge has a solution. JesseL’s excellent use of a regular expression made short work of that. But now … how to use that time stamp in your MATLAB alarm clock to compensate for inaccuracies in your system time? And perhaps more importantly, what is a reasonable tolerance for accuracy in this application?
Cheers
Bob
OK, so I started to implement this and ran into my first cotcha. The URL in question presents time in UTC (and other time zones), but matlab only seems to give time in local time. Is there a way to ask Matlab what timezone it believes it to be in?
Daniel, wonderful observation! Unfortunately, MATLAB relies on local system time. Different operating systems feature different interfaces for accessing system time. For example, on Windows you can use the legacy TIME (and DATE) functions.
Let’s see what other clever users suggest. :)
Well, when in doubt, guess. Assuming your system clock isn’t off by more than 1 hour and you aren’t in one of these half-hour time-zones, this will work.
I get the current time with the following:
% Get the current time in hours (12hour format). crnt = mod(now+getCurrentTimeDelta,0.5)*24; function [ timeDelta ] = getCurrentTimeDelta( timeZone ) %GETCURRENTTIMEDELTA Gets the offset in time of the system clock. %Time format is compatible with now. To get exact time, add the %return value to the current system time. As an optional argument, %supply the time-zone. URL = 'http://tycho.usno.navy.mil/cgi-bin/timer.pl'; atomTime = datenum(regexp(urlread(URL), ... '(.*)\sUTC','tokens','once'),'mmm. dd, HH:MM:SS'); sysTime = now; if ( nargin == 0 ) timeZone = 2; %The timezone in hours. timeDelta = atomTime - sysTime + timeZone/24; else % Assume time diff is less than 1 hour timeDelta = atomTime - sysTime; timeDeltaInHours = timeDelta*24; timeDelta = (timeDeltaInHours - round(timeDeltaInHours))/24; fprintf ( 'Time delta: %gs.\n', timeDelta*24*60*60 ); end end %EOFI also submitted the complete updated function to the file exchange, but it seems it will take a few days for it to be processed, so you will have to be patient. It has been sumbitted under the title “Atomic reminder”.
And here it is:
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=20693&objectType=file