简体   繁体   English

MATLAB:为什么函数无法在给定的日期输入中处理错误?

[英]MATLAB: Why can't the function handle the error in a given date input?

I'm studying MATLAB on coursera and stuck with this question: 我正在Coursera上学习MATLAB,并遇到了以下问题:

Write a function called day_diff that takes four scalar positive integer inputs, month1, day1, month2,day2. 编写一个名为day_diff的函数,该函数接受四个标量正整数输入,month1,day1,month2,day2。 These represents the birthdays of two children who were born in 2015. The function returns a positive integer scalar that is equal to the difference between the ages of the two children in days. 这些代表两个孩子在2015年出生的生日。该函数返回一个正整数标量,该整数等于两个孩子的天数之差。 Make sure to check that the input values are of the correct types and they represent valid dates. 确保检查输入值的类型正确,并且它们代表有效的日期。 If they are erroneous, return -1. 如果它们是错误的,则返回-1。 An example call to the function would be 该函数的示例调用为
dd = day_diff(1,30,2,1);
which would make dd equal 2. You are not allowed to use the built-in function datenum or datetime. 这将使dd等于2。不允许使用内置函数datenum或datetime。 Hint: store the number of days in the months of 2015 in a 12-element vector (eg, 31, 28, 31, 30 …) and use it in a simple formula. 提示:将2015年月份的天数存储在12个元素的向量中(例如31、28、31、30…),并以简单的公式使用它。

Multiple cases are tested using a pre-made evaluation code. 使用预制的评估代码测试多个案例。 My code so far: 到目前为止,我的代码:

function answer = day_diff(month1, day1, month2,day2)
    answer = -1;
    days_in_months = [31,28,31,30,31,30,31,31,30,31,30,31]; %days in every month array
    flag1 = days_in_months(month1); %to check if day1 is valid
    flag2 = days_in_months(month2); %to check if day2 is valid
    %Non valid values handing
    if nargin < 4
        error('Must have four arguments');
    end
    if ~isscalar(month1) || month1 < 1 || month1 ~= fix(month1) || month1 > 12
        error('month1 needs to be a positive integer and not greater than 12.');
    end
    if ~isscalar(month2) || month2 < 1 || month2 ~= fix(month2) || month2 > 12        
        error('month2 needs to be a positive integer and not greater than 12.');
    end
    if ~isscalar(day1) || day1 < 1 || day1 ~= fix(day1)|| day1 > flag1        
        error('day1 needs to be a positive integer and a valid date.');
    end
    if ~isscalar(day2) || day2 < 1 || day2 ~= fix(day2)|| day2 > flag2        
        error('day1 needs to be a positive integer and a valid date.');
    end
    %end of error handler
    %to find the age in days
    if (month1 == month2)
        inbetween_days = 0;
        if day1 == day2
            first_last_days = 0;
        elseif day1 < day2
            first_last_days = day2 - day1;
        else
            first_last_days = day1 - day2;
        end
    elseif month1 < month2
            inbetween_days = sum(days_in_months(month1+1:month2-1));
            first_last_days = (days_in_months(month1)-day1) + day2;
        else
            inbetween_days = sum(days_in_months(month2+1:month1-1));
            first_last_days = day1 + (days_in_months(month2)-day2);
    end
     answer = first_last_days +  inbetween_days;
end

so far so good but when it's day_diff(2, 29, 1, 22), the evaluation file gives an error instead of handling the wrong pre-defined input (day1 is 29 and maximum is 28), is the problem in how to return the -1 ? 到目前为止还不错,但是当它是day_diff(2,29,1,22)时,评估文件会给出错误,而不是处理错误的预定义输入(day1为29,最大值为28),这是如何返回的问题-1?

Problem 4 (day_diff):
        Feedback: Your function performed correctly for argument(s) 1, 30, 2, 1
        Feedback: Your function performed correctly for argument(s) 1, 1, 1, 1
        Feedback: Your function performed correctly for argument(s) 1, 1, 1, 2
        Feedback: Your function performed correctly for argument(s) 1, 2, 1, 1
        Feedback: Your function performed correctly for argument(s) 1, 1, 2, 1
        Feedback: Your function performed correctly for argument(s) 2, 1, 1, 1
        Feedback: Your function performed correctly for argument(s) 1, 31, 2, 1
        Feedback: Your function performed correctly for argument(s) 2, 1, 1, 31
        Feedback: Your function performed correctly for argument(s) 1, 1, 12, 31
        Feedback: Your function performed correctly for argument(s) 2, 1, 3, 1
        Feedback: Your function performed correctly for argument(s) 7, 1, 9, 30
        Feedback: Your program made an error for argument(s) 2, 29, 1, 22

    Your solution is _not_ correct.

Yes you never return -1 which they asked for. 是的,您永远不会返回他们要求的-1。 Remove the error messages and in those cases simply dont do anything and let your answer stay -1. 删除错误消息,在这种情况下,什么也不做,让您的答案保持-1。 The following is more lines of code then you had and you can improve upon it but this should pass the test you are failing 以下是您拥有的更多代码行,您可以对其进行改进,但这应该可以通过失败的测试

    function answer = day_diff(month1, day1, month2,day2)
    answer = -1;
    days_in_months = [31,28,31,30,31,30,31,31,30,31,30,31]; %days in every month array
    flag1 = days_in_months(month1); %to check if day1 is valid
    flag2 = days_in_months(month2); %to check if day2 is valid
    %Non valid values handing
    if nargin < 4
        answer=-1;

    elseif ~isscalar(month1) || month1 < 1 || month1 ~= fix(month1) || month1 > 12
        answer=-1;
    elseif ~isscalar(month2) || month2 < 1 || month2 ~= fix(month2) || month2 > 12        
        answer=-1;
    elseif ~isscalar(day1) || day1 < 1 || day1 ~= fix(day1)|| day1 > flag1        
        answer=-1;
    elseif ~isscalar(day2) || day2 < 1 || day2 ~= fix(day2)|| day2 > flag2        
        answer=-1;
    %end of error handler
    %to find the age in days
    elseif (month1 == month2)
        inbetween_days = 0;
        if day1 == day2
            first_last_days = 0;
        elseif day1 < day2
            first_last_days = day2 - day1;
        else
            first_last_days = day1 - day2;
        end
        answer = first_last_days +  inbetween_days;
    elseif month1 < month2
            inbetween_days = sum(days_in_months(month1+1:month2-1));
            first_last_days = (days_in_months(month1)-day1) + day2;
            answer = first_last_days +  inbetween_days;
    else
            inbetween_days = sum(days_in_months(month2+1:month1-1));
            first_last_days = day1 + (days_in_months(month2)-day2);
            answer = first_last_days +  inbetween_days;
    end

end

I would remove the answer = -1 in the first line and handle all the errors in one if block, to avoid errors in cases that may fall between the elseif blocks: 我将在第一行中删除answer = -1 ,并在一个if块中处理所有错误,以避免在elseif块之间可能出现的错误:

% Non valid values handing:
if (nargin ~= 4) ||...
        ~all([isscalar(month1) isscalar(month2) isscalar(day1) isscalar(day2)]) ||... % only scalars
        any([month1,month2,day1,day2]<1) ||... % all input greater then zero
        any(fix([month1,month2,day1,day2]) ~= [month1,month2,day1,day2]) ||... % all are integers
        any([month1,month2,day1,day2] > [12 12 flag1 flag2]) % all are valid dates
    answer = -1;
    return;
end
% end of error handler

if the condition is true, the function assigns -1 to answer and goes out (by return ). 如果条件为true,则该函数将-1分配给answer并退出(通过return )。

In my opinion, this way of error handling is simpler to follow and less error-prone, but I'm not sure that it will solve the original problem. 以我的观点,这种错误处理方式更易于遵循且不易出错,但是我不确定它将解决原始问题。

You could also remove the return statement, and put all the rest of the function in an else block, ie: 您还可以删除return语句,并将所有其余功能放在else块中,即:

if (nargin ~= 4) ||... % all the condition...           
    answer = -1;
else
    % all the calculation...
    answer = first_last_days +  inbetween_days;
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 matlab中的函数句柄错误 - Function handle error in matlab 为什么 Matlab 看不到我的函数? - Why can't Matlab see my function? Matlab无法处理数据 - Matlab can't handle data Matlab函数-要求用户输入给定参数 - Matlab Function - ask for user input for given argument 在 Matlab 中使用 fzero 时出错:未定义 function 或输入 arguments 类型为“function_handle”的方法“det” - Error using fzero in Matlab: Undefined function or method 'det' for input arguments of type 'function_handle' MATLAB:没有给出足够的输入参数错误 - MATLAB: Not enough input arguments error given 拟合的“ linearinterp”返回错误的Matlab集成“第一个输入参数必须是函数句柄” - Matlab integrate of fitted “linearinterp” returning error “First input argument must be a function handle” 给定子图的句柄,我可以在MATLAB中找到相关的子图索引吗? - Given a handle to a subplot, can I find the associated subplot index in MATLAB? Matlab:从函数外部检测函数句柄的输入自变量数量 - Matlab: Detect number of input arguments for function handle from outside of the function 类型为“ function_handle”的输入参数的未定义函数“ NR”。 MATLAB - Undefined function 'NR' for input arguments of type 'function_handle'. MATLAB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM