フィルターのクリア

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

1 回表示 (過去 30 日間)
Sam Lee
Sam Lee 2016 年 2 月 27 日
編集済み: Stephen23 2016 年 2 月 27 日
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 Center および File ExchangeManage Products についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by