Converting regular numbers into "time"

Hi everyone,
I have a question. If I define a vector like so:
TStart = 0; TEnd = 1000; TStep = 1; Time = TStart:TStep:TEnd;
Is there any way where I can convert this to date format, so that when I plot:
plot(Time,Data)
The x axis will have time stamps corresponding to the way I defined my Time array, and not just numbers?
I looked into datetick, but I'm not sure if I am on the right track here...

回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 4 月 12 日

0 投票

For datetick to work, the Time vector should be in the unit of full days. If, for example, your TStep was intended to reflect hours, then divide Time by 24

6 件のコメント

Sarah
Sarah 2012 年 4 月 13 日
So what would my plot end up looking like? Lets say:
TStart = 0;
TEnd = 1000;
TStep = 1;
Time = TStart:TStep:TEnd;
TStep is by seconds. So I have to divide my time vector by 86400? And will the plot reflect my data in days?
Walter Roberson
Walter Roberson 2012 年 4 月 13 日
Right. Suppose you wanted to show hours and minutes, then
plot(Time ./ 86400, Data);
datetick('x', 'HH:MM')
Sarah
Sarah 2012 年 4 月 14 日
Hmmm, I am still getting 00:00 for each tick on my plot. Does my data affect datetick?
Walter Roberson
Walter Roberson 2012 年 4 月 14 日
The data will not affect datetick.
Oleg Komarov
Oleg Komarov 2012 年 4 月 14 日
@Sarah: your result is inconsistent with the example.
If you run the example you provided, together with Walter's code, you should be getting 5 ticks: 00:00, 00:05, 00:10, 00:15 and 00:20.
Peter Wittenberg
Peter Wittenberg 2012 年 4 月 14 日
The 00:00 is correct for the time you put in. What's happening is that you are putting in a time stream so long that MATLAB is choosing tick mark dates that are integers, with each integer being a full day, starting at 00:00 when you put it in the HH:MM format. Adjust the time variable so that it covers a total time interval of less than one day and you'll see other numbers. For example, time =0:.001:1 will give you 1000 points spaced at 1/1000 of a day (1.44 minute) intervals

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

質問済み:

2012 年 4 月 12 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by