# Doug's MATLAB Video Tutorials

## Working with dates in MATLAB

I am a little obsessive about data visualization. I have a side project selling a book I wrote. The data is not in a nice time history form, so I need to manually gather the data then reformat and interpolate the time data. This shows some basic date processing in MATLAB.

### 5 Responses to “Working with dates in MATLAB”

1. Sean de Wolski replied on :

Trendy?

2. Doug replied on :

Problem is the data is hidden behind a log in page. Trendy can not get through.

3. John Gilmore replied on :

Do you know of any way to covert a date to the fiscal week/date? IE thursday july 18th, 2013 would be FW29.4.

4. Doug replied on :

@John

This is lightly tested, and I just made guesses as to how this algorithm works. That said, I think this does what you want. I just whipped it up. Test it, modify and enjoy.

Doug

function fiscalDate(inDateNum)

dayNum = weekday(inDateNum);

if (dayNum == 1) || (dayNum == 7)
error(‘Not a fiscal day’)
end

dayNum = dayNum – 1;

decimalDay = dayNum/10;

fiscalWeek = weeknum(inDateNum);

out = fiscalWeek + decimalDay;

5. Rodney replied on :

@Doug Being a numbers geek plus also being excited by seeing people use my code from the File Exchange, many years ago I wrote a script for scraping and visualising the number of downloads for an author’s File Exchange projects:
http://iheartmatlab.blogspot.com.au/2008/09/mathworks-file-exchange-profile_18.html

Sadly the File Exchange moved away from Total Downloads to number of downloads in the last 30 days. I guess I could always use the moving average and integrate over time…

On a related note to your post, one of the issues using date/time values on axes is that making the tick labels sensible is a difficult task!

In your video example I see you use ‘datetick’ to label the ticks with a datestr value. However if you then zoom into a portion of the graph, new tick values are NOT calculated.

Now to get around this I have in the past used the zoom mode callbacks to update the tick labels of an axes based on the new tick values. But this is clunky and ideally something MATLAB would handle itself.

The best and most generic way would be to have:
- an OnTickChange callback;
- have an additional property for X/Y/ZTickLabelFormat that you can give a sprintf style format string to (although the specifics of handling date values there are a little bit trickier)
- have a datex() function (like semilogx) which assumes X values to be date numbers and handles appropriate tick alignment / labels

Just my 2 (and a lot) cents

Rod

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 5 + 7 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

These postings are the author's and don't necessarily represent the opinions of MathWorks.