Extraction from the double cell?

2 views (last 30 days)
Alexandra Philip
Alexandra Philip on 6 Jul 2020
Edited: Alexandra Philip on 6 Jul 2020
I am having some trouble developing some error checking code. I first use:
I am particularly having trouble with displaying the code to ensure year, month, and day has a numerical value and a certain length and if they don't meet these requirements there will be an inputted error message. I think the problem ensures extracting the right order from the double as the year variable displays:
and the month double displays:
and the day double display:
Anyway to correct my code to display this as currently my code is displaying the error message even when there is a numerical value and the length is correct, this isn't suppose to happen, only when there isn't a numerical value and incorrect length(or number of elements in the number as year should have 4 and day and month should have 2) should the inputted error message be displayed.
  4 Comments
Voss
Voss on 6 Jul 2020
You would use strlength() to get the number of characters in a string, but that's not the problem here (since ERRORT is called with the character array Testdate, not the string Teststr).
Alexandra Philip
Alexandra Philip on 6 Jul 2020
Thank you. I used strlength() and the code ran smoothly.

Sign in to comment.

Answers (1)

Voss
Voss on 6 Jul 2020
There seems to be some confusion about which variables are character arrays and which are doubles. For instance, in this line:
if Testdate(7)<1 || ~ischar(year)==1 || isempty(year)==1 || (length(year)~=4) || year<0
Testdate(7) is a character, and year is a double. In the first comparison (Testdate(7)<1), you are comparing a character with the double 1, which compares the ASCII codes, which is probably not what you want to be doing (in particular the ASCII code for '0' is 48 so if Testdate(7) were '0', then Testdate(7)<1 would evaluate to false, since 48 is not less than 1). I think you are intending to check whether the first digit of the year is less than 1, which you can do by checking if year<1000.
Also, note that year comes from str2double, which always returns a scalar value (possibly NaN). Therefore, ischar(year) will always be false (because year is a double), isempty(year) will also always be false (because year is a scalar), and length(year)~=4 will always be true (because year is a scalar). Also note that length(yearstr) will always be 4 by construction (since yearstr = Testdate(7:10) is always 4 characters long).
So maybe the following check would be sufficient to test for a valid year:
if isnan(year) || year < 1000
Similarly, the checks with isempty(month), ischar(month), length(month), isempty(day), isnumeric(day), are unnecessary or redundant. Since they all come from str2double, they are guaranteed to be scalar doubles, so you only need to check for NaN and check that the value is in an appropriate range.
  1 Comment
Voss
Voss on 6 Jul 2020
Another consideration is that after the user is prompted to enter a new date, only subsequent tests are performed on the new date. I mean, the test that was failed (and all previous tests) is not done on the new date. A more robust approach would be to prompt the user for a date in a while loop that iterates until all tests are passed.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!