I don't understand where I'm going wrong
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2024-1-27
编辑:Stephen23
2024-1-27
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.
回答(1 个)
Sai Teja G
2024-1-26
编辑:Sai Teja G
2024-1-26
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 个评论
Walter Roberson
2024-1-27
if month == 2
if ~(1<=day&&day<=28)
valid=false;
end
If the condition does not hold, you do not set valid to true
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!