Plotting data against time

14 次查看(过去 30 天)
Edit: I've added the .mat file and a code which shows what i'm trying to do.
% timex=datetime(time_orginal,'Format','dd/MM/yy HH:mm:ss:SSS') does not
% work and results in a error. Hence the loop.
for i=1:length(time_orginal)
timex(i)=datetime(time_orginal(i),'Format','dd/MM/yy HH:mm:ss:SSS')
end
plot(timex,datax)
Using the .mat file provided and this code results in the following plot:
----------------------
Hi all. I have two arrays that i'm trying to plot together. Timex is a 10x1 datetime and datax is 10x1 double.
when I do
plot(timex,datax)
I dont get what i'm after, which is date and time on x axis and data on y axis.
I dont understand why the datetime is not showing up on the x-axis. Any help is much appreciated.
the data:
timex =
18/09/21 04:32:02:000
18/09/21 04:32:02:200
18/09/21 04:32:02:400
18/09/21 04:32:02:600
18/09/21 04:32:02:800
18/09/21 04:32:03:000
18/09/21 04:32:03:200
18/09/21 04:32:03:400
18/09/21 04:32:03:600
18/09/21 04:32:03:800
and
datax =
-0.0200
-0.0200
-0.0300
-0.0200
-0.0200
-0.0200
-0.0200
-0.0300
-0.0300
-0.0300
  2 个评论
Walter Roberson
Walter Roberson 2021-9-21
plot(t,x)
That involves two variables that we do not know anything about. You have told us about timex and datax but not about t and x
Andy Thor
Andy Thor 2021-9-21
whoops. That was supposed to be plot(timex,datax) . Edited.

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2021-9-21
编辑:the cyclist 2021-9-21
New answer based on your update.
The problem is that your original time series is in two different formats -- not including the milliseconds when the time is exact to the second.
I'm frankly not certain how datetime() handled the conversion in this case, but if you compare your version of timex and mine, you'll see the difference.
There may be a more elegant way to handle that, but this works in a kludgy way:
load noGoodPlot.mat
timex = datetime(time_orginal, 'InputFormat',"dd/MM/yy HH:mm:ss:SSS");
timex(1:5:end) = datetime(time_orginal(1:5:end),'InputFormat',"dd/MM/yy HH:mm:ss");
figure
plot(timex,datax);
Ideally, the solution to this would resolve the data format issue upstream, in time_orginal.

更多回答(1 个)

the cyclist
the cyclist 2021-9-21
编辑:the cyclist 2021-9-21
Works for me, when I correctly specify valid dates using the input format.
I'm guessing that your datetime is somehow messed up. It would be helpful if you uploaded your variables in a MAT file (using the paperclip icon), so we can see what you are actually working with.
timex = datetime( ...
{'18/09/21 04:32:02:000'; ...
'18/09/21 04:32:02:200'; ...
'18/09/21 04:32:02:400'; ...
'18/09/21 04:32:02:600'; ...
'18/09/21 04:32:02:800'; ...
'18/09/21 04:32:03:000'; ...
'18/09/21 04:32:03:200'; ...
'18/09/21 04:32:03:400'; ...
'18/09/21 04:32:03:600'; ...
'18/09/21 04:32:03:800'},'InputFormat','dd/MM/yy HH:mm:ss:SSS');
datax = [ ...
-0.0200;
-0.0200;
-0.0300;
-0.0200;
-0.0200;
-0.0200;
-0.0200;
-0.0300;
-0.0300;
-0.0300];
figure
plot(timex,datax)
  1 个评论
Andy Thor
Andy Thor 2021-9-21
Thanks for the reply. I´ve added some more information and the .mat file.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by