Azzera filtri
Azzera filtri

while loop for a date input

2 visualizzazioni (ultimi 30 giorni)
Lauren Hough
Lauren Hough il 8 Mag 2020
Commentato: Isiah Pham il 12 Mag 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

Risposte (1)

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

Accedi per commentare.

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by