while loop for a date input

2 次查看(过去 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 CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by