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!