簡體   English   中英

在 Matlab 中獲得兩個輸出而不是一個

[英]Getting two outputs instead of one in Matlab

這里的問題是在不使用本機 Matlab 日期函數的情況下查找提供的日期是否有效。 我希望有人能在這里指出我的錯誤。 當我提交時,我還在 Matlab 學習工具中收到“在調用valid_date期間未分配輸出參數valid (可能還有其他參數)”錯誤。

function valid = valid_date(year,month,day)

if nargin~=3
    valid = false;
elseif ~isscalar(year)||year<1||year~=fix(year)
    valid = false;
    return
elseif ~isscalar(month)||month<1||month~=fix(month)
    valid = false;
    return
elseif ~isscalar(day)||day<1||day~=fix(day)
    valid = false;
    return
elseif month>12 || day > 31
    valid = false;
end

if ((mod(year,4)==0 && mod(year,100)~=0) || mod(year,400)==0)
    leapdata=1;
else
    leapdata=0;
end

%the below if statements are used to define the months. Some months have 
%31 days and others have 30 days, while February has only 28 days and 29 on
%leap years. this is checked in the below code.
% I feel the below code is where the error is.

if ismember (month, [1 3 5 7 8 10 12])
    ismember (day, (1:31))
    return
elseif ismember( month, [4 6 9 11])
    ismember (day, (1:30))
    return
end


if month == 2
    if leapdata==1
        ismember (day, (1:29))
        return
    elseif leapdata==0
        ismember (day, (1:28))
        return
    else
        valid = false;
    end 
end

在 Matlab function 結束時返回時,變量valid的值作為 output 發送。 在四個注釋下面的行中,您需要在 if 語句中將變量分配為 true 或 false。 例如:

if ismember(month, [1 3 5 7 8 10 12])
    valid = ismember(day, (1:31))
    return
elseif ismember(month, [4 6 9 11])
    valid = ismember(day, (1:30))
    return
end


if month == 2
    if leapdata == 1
        valid = ismember(day, (1:29))
        return
    elseif leapdata == 0
        valid = ismember(day, (1:28))
        return
    else
        valid = false;
    end 
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM