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.
采纳的回答
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 个评论
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

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!
.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
