繁体   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