简体   繁体   English

在 Matlab 中获得两个输出而不是一个

[英]Getting two outputs instead of one in Matlab

The question here was to find if the date provided is a valid one or not without using native Matlab date functions.这里的问题是在不使用本机 Matlab 日期函数的情况下查找提供的日期是否有效。 I was hoping if someone can point out my mistake here.我希望有人能在这里指出我的错误。 I'm also getting the "Output argument valid (and maybe others) not assigned during call to valid_date " error in Matlab learning tool when I submit it.当我提交时,我还在 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

When returning at the end of a Matlab function, the value of the variable valid is sent as an output.在 Matlab function 结束时返回时,变量valid的值作为 output 发送。 In the lines below the four comments, you need to assign the variable to true or false inside of the if statements.在四个注释下面的行中,您需要在 if 语句中将变量分配为 true 或 false。 For example:例如:

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