while loop for a date input

조회 수: 1 (최근 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