Error: Output argument "__" (and maybe others) not assigned during call to "__"
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sam Lee
am 27 Feb. 2016
Bearbeitet: Stephen23
am 27 Feb. 2016
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?
0 Kommentare
Akzeptierte Antwort
Star Strider
am 27 Feb. 2016
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
0 Kommentare
Weitere Antworten (1)
Stephen23
am 27 Feb. 2016
Bearbeitet: Stephen23
am 27 Feb. 2016
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Manage Products finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!