WHY CAN'T I PLOT THIS WITH "DATETIME"?

Hello everyone,
I can't plot because of the error with "data time".
Does anyone know how I can solve this problem.
Thank you.

 Accepted Answer

The first column is not a valid datetime array. It first needs to be converted to one.
Try this —
LD = load('LARSEN_MMEQ1.mat');
LARSENMMEQ1 = LD.LARSENMMEQ1;
LARSENMMEQ1.Var1 = datetime(LARSENMMEQ1.Var1, 'InputFormat','MM/dd/yyyy');
x = LARSENMMEQ1.Var1;
y = LARSENMMEQ1.Var2;
figure
plot(x, y)
grid
producing —
.

8 Comments

Thank you.
But why do I get a line if I try to do this multiplication?
NEW=LARSENMMEQ1.Var2*10*0.35;
I have no idea. It works correctlly foir me —
LD = load('LARSEN_MMEQ1.mat');
LARSENMMEQ1 = LD.LARSENMMEQ1;
LARSENMMEQ1.Var1 = datetime(LARSENMMEQ1.Var1, 'InputFormat','MM/dd/yyyy');
x = LARSENMMEQ1.Var1;
y = LARSENMMEQ1.Var2*10*0.35;
figure
plot(x, y)
grid
So I guess the question is what exactrly are you plotting?
Note — In the plot you posted in your Comment the independent variable is obviously not the datetime variable, so it may be that you are plotting 'NEW' as a funciton of ‘Var2’ (or the reverse of that). They would be linearly related, with an intercept of 0 and a slope of 3.5 (or the inverse of that).
.
Thank you for your help!
But I can't get why the error still occurs:
"Unable to convert the text to datetime using the format 'dd/MM/yyyy'."
I also tried with those attached (that are similar) and it doesn't work.
My pleasure!
Of course the previous datetime import and conversion will not work, because the format is entirely different!
Try this —
LD = load('PAOLA_MMEQ1.mat');
PAOLAMMEQ = LD.PAOLAMMEQ;
dv = regexp(PAOLAMMEQ.Var1, '\d*\-\w*\-\d*','match');
PAOLAMMEQ.Var1 = datetime([dv{:}], 'InputFormat',"dd-MMM-yyyy").';
x = PAOLAMMEQ.Var1;
y = PAOLAMMEQ.Var2;
figure
plot(x, y)
grid
This required the additional step of using a simple regexp call to extract the date srtings.
If you want to interpolate the missing data, use the fillmissing function, since there are 3008 NaN values in ‘y’. There are some anomalous values in Sep and Oct 2016, so you will have to decide on how to deal with them, so filloutliers and related functions are worth considering as well.
.
Thank you very much for your help and explanation.
Just a quick clarification: I can only see NaN values in September and October 2016; so what kind of anomalous values do you mean?
My pleasure.
There are no missing ‘x’ (datetime) values, so changing the plot to:
ym = ismissing(y);
yf = fillmissing(y,'linear');
figure
hpm = plot(x(ym), yf(ym), '.r', 'MarkerSize',3);
hold on
hpd = plot(x, y, '.-b');
hold off
grid
legend([hpd,hpm],'Data','Missing', 'Location','best')
shows the ‘y’ data in blue and all 3008 missing NaN values in red —
The anomalous values are the descending ‘spike’ in Sep-Oct 2016.
.
Thank you very much.
You've been very kind.
As always, my pleasure!
.

Sign in to comment.

More Answers (0)

Asked:

Pul
on 20 Jul 2021

Commented:

on 21 Jul 2021

Community Treasure Hunt

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

Start Hunting!