MATLAB Answers

(2016a) How to plot datetime versus numbers? Error: "Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or data inputs which can be converted to double."

13 views (last 30 days)
Jane Fowler
Jane Fowler on 10 Jul 2020
Answered: Jane Fowler on 10 Jul 2020
I'm using Matlab 2016a and I am relatively new to Matlab. I have imported data from a large external file. The first column is datetime in the following format:
2015-07-01 01:00:00
The other columns are all numbers. E.g.,
1100.0
I want to plot the datetime data as X and numbers as Y. I have tried the following, where DT_1 is the column of imported datetime data and n_raw is a column of numbers:
DT_1_test = datetime(DT_1, 'InputFormat', 'yyyy-MM-dd HH:mm:ss')
t = datetime(DT_1_test);
DateNumber = datenum(t)
plot(DateNumber, n_raw);
There are two problems.
  1. My screen output for datetime is in the wrong format. Example: 10-Jul-2019 23:00:00
  2. Also, I get this error: "Data inputs must match the axis configuration. A numeric axis must have numeric data inputs or data inputs which can be converted to double."
I would be very grateful for any help. I have not been able to get past it all day. Thank you.

  1 Comment

Jane Fowler
Jane Fowler on 10 Jul 2020
Update
I tried the following and it solved part of my problem:
DT_1_test = datetime(DT_1, 'InputFormat', 'yyyy-MM-dd HH:mm:ss')
t = datetime(DT_1_test);
plot(datenum(t), n_raw);
It gave me a plot!
  1. But I still have the problem that the date is not in the right format. My screen output shows the dates as "10-Jul-2019 23:00:00" and NOT as "yyyy-MM-dd HH:mm:ss" (what I need).
  2. Also, the X axis values are appearing as numbers, not dates.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 10 Jul 2020
For your first question, you've specified the format that datetime should use to interpret the representation of the time that you passed in, but you didn't specify the format in which the created datetime should be displayed. To do that, specify the 'Format' name-value pair argument (just like you specified 'InputFormat', with the desired format as the value in that pair.
dt1 = datetime('now')
dt2 = datetime('now', 'Format', 'yyyy-MM-dd HH:mm:ss')
For your second question, if you've previously plotted a line whose x data is numeric, you can't add another line to that same axes whose x data is a datetime.
x = 0:360;
y = sind(x);
plot(x, y)
hold on
x2 = datetime('today') + days(x);
plot(x2, y) % won't work
For this particular case, there's a reasonably intuitive mapping between x and x2. But in general there wouldn't be. In those cases answering a question like "Does x = 28 correspond to a week from now?" to line up the two plots could be difficult.

  1 Comment

Jane Fowler
Jane Fowler on 10 Jul 2020
Thank you very much, Steven.
First, I am very happy to find that this worked to format the screen output correctly:
DT_1_test = datetime(DT_1, 'InputFormat', 'yyyy-MM-dd HH:mm:ss', 'Format', 'yyyy-MM-dd HH:mm:ss')
Regarding your second point, I haven't previously plotted any x data that are numeric, so I'll keep working on this issue.
Thanks again very much for your help with that first point!

Sign in to comment.

More Answers (1)

Jane Fowler
Jane Fowler on 10 Jul 2020
OK, finally figured it out. I'll post my solution here because there are bound to be other newbies like me who have the same problem.
1. Steven solved my first problem, the formatting of the on-screen datetime and therefore I'll mark his answer correct.
2. I came across a forum post elsewhere where someone suggested using "datetick" to convert the x axis numbers to dates (see below).
DT_1_test = datetime(DT_1, 'InputFormat', 'yyyy-MM-dd HH:mm:ss', 'Format', 'yyyy-MM-dd HH:mm:ss')
t = datetime(DT_1_test);
plot(datenum(t), n_raw);
datetick('x', 'yyyy-mm-dd')

  0 Comments

Sign in to comment.