MATLAB: (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.”

datenumdatetimeplot

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.

Best Answer

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.