Still using a switch structure, now within a function. I keep getting an error message saying I don't have enough input arguments for (line 6).

조회 수: 1 (최근 30일)
function total_days = total(month,day,extra_day)
%CalenderCalculations.m this programm computes the total elapsed days in a
%year given the number (1-12) of the month, the day, and an indication of
%whether the year is a leap year
%Created on October 19, 2016 By C.Hickman
total_days = day;
k = 1: month-1
switch 'k'
case{1,3,5,7,8,10,12}
total_days = total_days + 31;
case{4,6,9,11}
total_days = total_days + 30;
case 2
total_days = total_days + 28 + extra_day;
end
month = input('Enter month (1-12):');
day = input('Enter day (1-31): ');
extra_day = input ('Enter 1 for leap year; 0 otherwise; ');
total_days = total(month,day,extra_day)
end

채택된 답변

Walter Roberson
Walter Roberson 2016년 10월 19일
You should almost always switch() on an expression rather than a constant like 'k' (which is a character vector)
switch() applied on a vector will only work if the vector is a character vector (that is, a string)
You need a loop or something like that.
function total_days = total(month,day,extra_day)
[...]
total_days = total(month,day,extra_day)
that last line would be recursive call to the function you are in.
You need to move the prompts to a different file and run that file instead of trying to run total directly.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by