Timestamp on X axis from datetime

58 ビュー (過去 30 日間)
Maksym Zawrotny
Maksym Zawrotny 2018 年 12 月 3 日
編集済み: Jorik 2022 年 6 月 21 日
Hello, I have made a plot already which looks like that:plot1.PNG
X axis is a time of observation. It is a vector of [0, 5, 10, 15 ...]. I would like to use a timestemps as a X axis ticks. I have a variable ts, which is the timestamp of each sample. How should I change my X axis description using a plot browser to timestams. Format of my timestamps is 'dd.MM.yyyy HH:mm:ss'

採用された回答

Jorik
Jorik 2018 年 12 月 3 日
Hi Maksym, if you convert your x vector to a datetime, you can simply plot the datetime vector against your values for the 3 lineplots and already get meaningful labels for the timestamps.
Example:
x = today + (0.5:0.01:0.6);
x_datetime = datetime(x, 'ConvertFrom', 'datenum');
plot(x_datetime, rand(size(x)))
As you can see, I start with simple datenum doubles, convert it to datetimes and if you zoom into the plot or use a different time-scale, the x-labels are automatically changed to show meaningful values and details.
If you need the specific format you mentioned, you can specify it with a line like this:
xtickformat('dd.MM.yyyy HH:mm:ss')
All of this is possible since R2016b or do you need to use an older version of MATLAB?
  4 件のコメント
Laukik Avinash Kharche
Laukik Avinash Kharche 2022 年 6 月 21 日
Hello,
I have a question regarding the answer you gave. I use it for a quite similar purpose as the questioner here. But when I use your code for getting x_datetime, The date I see there is 31-Dec--0001. I wonder how I could put the correct date there ? I have the Time stamp in decimals(only for time and not the date) and they get converted to DD-MMM--YYYY hh:mm:ss after using the date time function.
Thank you,
Laukik.
Jorik
Jorik 2022 年 6 月 21 日
編集済み: Jorik 2022 年 6 月 21 日
You either need to choose a date (I would only do this is you knew which day the data is referring to) or use a different datatype. You could e.g. use duration instead of datetime to leverage the smart label capabilities without having actual dates.
E.g. if you want to plot a sine for t = 0-60 seconds duration:
t = 0:0.1:60; % seconds
f = 0.05; % freq
v = sin(2*pi*f*t); % calculate the sin(2πft)
t_d = duration(seconds(t));
plot(t_d, v) % plot t as duration against v

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by