필터 지우기
필터 지우기

Error: Output argument "__" (and maybe others) not assigned during call to "__"

조회 수: 1 (최근 30일)
I wrote this simple code of a program that receives input from the user and calculates the parking bill following certain rules. The code includes two functions. I keep getting this error whenever the main function calls the second one:
Error in longTerm (line 2)
if mins<=60
Output argument "bill" (and maybe others) not assigned during call to "longTerm".
Error in assignment1q3 (line 15)
bill = longTerm(mins);
This is the part of the main function (assignment1q3) where function (longTerm) is called:
switch(lot)
case {'lt','LT'}
bill = longTerm(mins);
case {'st','ST'}
bill = shortTerm(mins);
otherwise
disp('Invalid input')
end
this is the function called:
function bill = longTerm(mins)
if mins<=60
bill = 1;
elseif mins<=1440 && mins>60
bill = 1 + mins/60;
if mins<=1440 && bill>6;
bill = 6;
elseif mins<=10080 && bill>42
bill = 42;
end
elseif mins>10080
bill = 42;
end
end
I tried changing variable names of the functions but I still got the same error. What should I do?

채택된 답변

Star Strider
Star Strider 2016년 2월 27일
Assign a default value for bill first. It will prevent that error and will also help you troubleshoot your code:
function bill = longTerm(mins)
bill = NaN;
if mins<=60
. . . CODE . . .
end

추가 답변 (1개)

Stephen23
Stephen23 2016년 2월 27일
편집: Stephen23 2016년 2월 27일
The problem is simple. In your function you define bill for each of these cases:
function bill = longTerm(mins)
if mins<=60
...
elseif mins<=1440 && mins>60
...
elseif mins>10080
...
end
end
But what happens when mins>1440 && min<=10080 ? In this case bill is not defined. The easiest solution would be to simply add an else case to the end of your if / elseif statements:
... your code
else
bill = NaN;
end

카테고리

Help CenterFile Exchange에서 Manage Products에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by