while loop for a date input

2 vues (au cours des 30 derniers jours)
Lauren Hough
Lauren Hough le 8 Mai 2020
Commenté : Isiah Pham le 12 Mai 2020
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

Réponses (1)

Isiah Pham
Isiah Pham le 8 Mai 2020
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 commentaires
Lauren Hough
Lauren Hough le 9 Mai 2020
Changed it to this, still giving me the first error no matter what is inputted
Isiah Pham
Isiah Pham le 12 Mai 2020
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by