MATLAB Answers

Plotting data against time

조회 수: 53(최근 30일)
Andy Thor
Andy Thor 2021년 9월 21일 14:54
댓글: Andy Thor 2021년 9월 21일 20:25
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
Andy Thor
Andy Thor 2021년 9월 21일 15:12
whoops. That was supposed to be plot(timex,datax) . Edited.

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2021년 9월 21일 16:12
편집: the cyclist 2021년 9월 21일 16: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
Andy Thor
Andy Thor 2021년 9월 21일 20:25
Thank you for your help in this matter.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

the cyclist
the cyclist 2021년 9월 21일 15:13
편집: the cyclist 2021년 9월 21일 17:18
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일 15:32
Thanks for the reply. I´ve added some more information and the .mat file.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by