VALID_DATE FUNCTION PROBLEM : Write a function called valid_date that takes three positive integer scalar inputs year, month, day. If these three represent a valid date, return a logical true, otherwise false...

1 回表示 (過去 30 日間)
Emirhan Bilgiç
Emirhan Bilgiç 2020 年 10 月 31 日
編集済み: Rik 2020 年 11 月 3 日
function [valid] = valid_date(y,m,d)
if ~isscalar(y) || ~isscalar(m) || ~isscalar(d)
valid= false;
end
if m > 12
valid= false;
elseif d > 31
valid= false;
end
if mod(y,4)==0
valid= valid_date(y,2,29);
valid= true;
end
if mod(y,400)==0 && mod(y,100)==0
valid= valid_date(y,2,29);
valid=false;
end
if m== 1 || 3 || 5 || 7 || 8 || 10 || 12
if d== 31
valid= true;
end
elseif m== 4 || 6 || 9 || 11
if d== 31
valid= false;
end
elseif m== 2
if d>=30
valid= false;
end
end
  1 件のコメント
Emirhan Bilgiç
Emirhan Bilgiç 2020 年 10 月 31 日
編集済み: Emirhan Bilgiç 2020 年 10 月 31 日
I keep seeing the error of "Output argument 'valid' (and maybe others) not assigned during call to 'valid_date'." Didn't i assign? Where am i missing? Thanks in advance ♥

サインインしてコメントする。

回答 (1 件)

Anmol Dhiman
Anmol Dhiman 2020 年 11 月 3 日
編集済み: Anmol Dhiman 2020 年 11 月 3 日
Hi Emirhan ,
You need to initialize valid in the function. Add the following statement after creating function
function [valid] = valid_date(y,m,d)
valid = true; % or valid = false;
Regards,
Anmol Dhiman

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by