while loop for a date input

3 ビュー (過去 30 日間)
Lauren Hough
Lauren Hough 2020 年 5 月 8 日
コメント済み: Isiah Pham 2020 年 5 月 12 日
I've a section of code to display one error if an inputted date is less than 22nd jan 2020 and another if its greater than 22nd april 2020.
It gives the first error no matter what date is inputted.
What needs correcting?
Thanks
date1 = 0 ;
Variable3 = input('Please enter a date after 22/01/2020 in the format dd/mm/yyyy \n');
DateString = { 'Variable3' } ;
FormatIn = 'dd/mm/yyyy' ;
datenum('Variable3', 'FormatIn') = date1 ;
if (date1 < 737812) % 737812 = datenum for 22/01/2020
fprintf('Error 505: No recorded cases.\n Please enter a date after 22/01/2020');
end
if (date1 > 737903) % 737903 = datenum for 24/04/2020
fprint('Error 231: No data currently available. \n Please enter a date before 25/04/2020 or check back after next update.');
end

回答 (1 件)

Isiah Pham
Isiah Pham 2020 年 5 月 8 日
When you assign variables, it's
variable = action;
When you assigned date1 to a 0, it's stays that way. What you want is date1 = datenum('Variable3', 'FormatIn');
  2 件のコメント
Lauren Hough
Lauren Hough 2020 年 5 月 9 日
Changed it to this, still giving me the first error no matter what is inputted
Isiah Pham
Isiah Pham 2020 年 5 月 12 日
It might be because datenum is taking in literaly 'Variable3', the character vector. Change it to just datenum(DateString, FormatIn)
You also don't need a cell array for a single input, so you can get rid of the curly brackets

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

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by