I don't understand where I'm going wrong
1 view (last 30 days)
Show older comments
function valid=valid_date(year,month,day)
if nargin~=3
valid=false;
end
if ~isscalar(year) || year<1 || year~= fix(year)
valid=false;
elseif ~isscalar(month) || month<1 || month~= fix(month) || month>12
valid=false;
elseif ~isscalar(day) || day<1 || day~= fix(day) || month>31
valid=false;
end
% leap year
if mod(year,4)==0 && mod(year,100)~= 0 || mod(year,400)== 0
if month==2
if ~(1<=day&&day<=29)
valid=false;
end
elseif month ~= (1||3||5||7||8||10||12)
if ~(1<=day&&day<=30)
valid=false;
end
else
valid=true;
end
end
if month == 2
if ~(1<=day&&day<=28)
valid=false;
end
elseif month ~= (1||3||5||7||8||10||12)
if ~(1<=day&&day<=30)
valid=false;
end
else
valid=true;
end
4 Comments
Stephen23
on 27 Jan 2024
Edited: Stephen23
on 27 Jan 2024
Note that || is a bivariate operator: it is a function with exactly two inputs and has exactly one output. You cannot chain it into arbitrarily long lists of values and expect it to operate on all of the values simultaneously. So your code:
(1||3||5||7||8||10||12)
is exactly equivalent to writing
((((((1||3)||5)||7)||8)||10)||12)
which because all of the values are non-zero is basically equivalent to
((((((true||true)||true)||true)||true)||true)||true)
which is exactly equivalent to
true
So your code
month ~= (1||3||5||7||8||10||12)
reduces down to
month ~= true
which because TRUE is equivalent to 1 your code is exactly equivalent to
month ~= 1
In short: your invented syntax does not do what you think it does and will not work.
Tip: use ISMEMBER instead. It works.
Answers (1)
Sai Teja G
on 26 Jan 2024
Edited: Sai Teja G
on 26 Jan 2024
Hi Shruti,
I assume that you are facing the "not enough input arguments" error, it occurs because the function is being executed without supplying the necessary arguments; in other words, no input arguments are provided when the function is called. This is why the error appears during the evaluation of the second "if" condition. To rectify this issue, you can introduce a `return` statement at the end of the first "if" condition. Additionally, it's important to note that the variable `valid` has not been initialized prior to the check for the number of input arguments (`nargin`). If the correct number of arguments is not provided, the variable `valid` will remain undefined, potentially leading to further errors.
function valid=valid_date(year,month,day)
valid=true;
if nargin~=3
valid=false;
return; % exit your code if there are not enough arguments
end
if ~isscalar(year) || year<1 || year~= fix(year)
valid=false;
elseif ~isscalar(month) || month<1 || month~= fix(month) || month>12
valid=false;
elseif ~isscalar(day) || day<1 || day~= fix(day) || month>31
valid=false;
end
% leap year
if mod(year,4)==0 && mod(year,100)~= 0 || mod(year,400)== 0
if month==2
if ~(1<=day&&day<=29)
valid=false;
end
elseif month ~= (1||3||5||7||8||10||12)
if ~(1<=day&&day<=30)
valid=false;
end
else
valid=true;
end
end
if month == 2
if ~(1<=day&&day<=28)
valid=false;
end
elseif month ~= (1||3||5||7||8||10||12)
if ~(1<=day&&day<=30)
valid=false;
end
else
valid=true;
end
Hope this clarifies your doubt!
3 Comments
Walter Roberson
on 27 Jan 2024
if month == 2
if ~(1<=day&&day<=28)
valid=false;
end
If the condition does not hold, you do not set valid to true
See Also
Categories
Find more on Time Series Objects in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!