MATLAB Answers

How can I apply if else statement to exclude dates?

7 ビュー (過去 30 日間)
Nuria Andreu
Nuria Andreu 2021 年 6 月 9 日
コメント済み: Nuria Andreu 2021 年 6 月 15 日
I have many data files with dates ranging from the 1800s to 2021. I only need dates from 1/1/1940 to 12/31/2020. I need to maintain the data file as untouched as possible (this is why I did not delete the extra dates). Can anyone help me with this code?
I appreciate the help!
Thank you!

採用された回答

ytzhak goussha
ytzhak goussha 2021 年 6 月 10 日
if vec is your date string vector:
vec = ["12/31/2020";"12/30/2020";"12/29/2020"]
then you must first convert it to date class:
date_vec = datetime(vec,'InputFormat','MM/dd/yyyy')
this will give the output:
date_vec =
3×1 datetime array
31-Dec-2020
30-Dec-2020
29-Dec-2020
and then you can either get a logical array with your condition:
upper_limit = datetime("12/31/2020",'InputFormat','MM/dd/yyyy');
lower_limit = datetime("12/30/2020",'InputFormat','MM/dd/yyyy')
logical array = (date_vec>lower_limit&date_vec<upper_limit)
or you can do if else statements
if this_date > upper_limit
else
end
  1 件のコメント
Nuria Andreu
Nuria Andreu 2021 年 6 月 15 日
Thank you! it worked perfectly

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by