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.
採用された回答
Star Strider
2021 年 7 月 20 日
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 件のコメント
Pul
2021 年 7 月 20 日
Thank you.
But why do I get a line if I try to do this multiplication?
NEW=LARSENMMEQ1.Var2*10*0.35;

Star Strider
2021 年 7 月 20 日
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).
.
Pul
2021 年 7 月 20 日
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.
Star Strider
2021 年 7 月 20 日
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.
.
Pul
2021 年 7 月 21 日
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?
Star Strider
2021 年 7 月 21 日
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.
.
Pul
2021 年 7 月 21 日
Thank you very much.
You've been very kind.
Star Strider
2021 年 7 月 21 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
