Plotting data against time

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 Comments

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
whoops. That was supposed to be plot(timex,datax) . Edited.

Sign in to comment.

 Accepted Answer

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.

More Answers (1)

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 Comment

Thanks for the reply. I´ve added some more information and the .mat file.

Sign in to comment.

Products

Release

R2021a

Asked:

on 21 Sep 2021

Commented:

on 21 Sep 2021

Community Treasure Hunt

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

Start Hunting!