Plotting data against time
15 ビュー (過去 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
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
採用された回答
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
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)
参考
カテゴリ
Help Center および File Exchange で Time Series Events についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

