Output argument "cost" (and maybe others) not assigned during call to "C:\Users\​NIck\Docum​ents\MATLA​B\calcCall​.m>calcCal​l".

조회 수: 2 (최근 30일)
Can anyone tell me what I"m doing wrong, I've read other threads and can't figure this out. I made a function calcCall to determine the cost of a call during given times of the day. tod = time of day; doc = duration of call. I created a new function window and I think thats where the problem lies because when I take the same code and write it on main, it works fine. So there is a disconnect between main that the function call I guess. Thanks for your help.
in main:
%%LAB6 HMWK
% DESCRIPTIVE TEXT
tod = input('What time of day was the call made, day evening or night?','s');
doc = input('How long did the call last?');
ans = calcCall('tod',doc);
fprintf('The cost of the call is %.2f, ', ans)
the function file:
function [ cost ] = calcCall(tod,doc) %must save this function with name 'calcCall'
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
switch tod
case {'day' 'Day' 'DAY' 'd' 'D'}
if (doc > 0 && doc < 10)
cost = ceil(doc)*.10;
elseif (doc >= 10 && doc < 30)
cost = 1 + ceil(doc)*.08;
else
cost = 2.60 + ceil(doc)*.06;
end
case {'evening' 'Evening' 'EVENING' 'e' 'E'}
if (doc > 0 && doc < 10)
cost = ceil(doc)*.07;
elseif (doc >= 10 && doc < 30)
cost = .70 + ceil(doc)*.05;
else
cost = 1.70 + ceil(doc)*.04;
end
case {'night' 'Night' 'NIGHT' 'N' 'n'}
if (doc > 0 && doc < 10)
cost = ceil(doc)*.04;
elseif (doc >= 10 && doc < 30)
cost = .04 + ceil(doc)*.03;
else
cost = 1.00 + ceil(doc)*.02;
end
end
end
Command line:
EDU>> clear EDU>> which calcCall C:\Users\NIck\Documents\MATLAB\calcCall.m EDU>> which HMWKLab6 C:\Users\NIck\Documents\MATLAB\HMWKLab6.m EDU>>

답변 (2개)

Brendan Hamm
Brendan Hamm 2015년 3월 2일
편집: Brendan Hamm 2015년 3월 2일
In the function you expect the variable called tod to assume one of the values in {'day','evening','night'}, along with alternative ways of entering these 3 cases.
In your call to the function you pass it the character array tod = 'tod'
ans = calcCall('tod',doc);
% should read
ans = calcCall(tod,doc);
Just remove the the quotation marks and you will pass in the variable tod which has the value from the input.
Note: If you have a discrete number of choices, as in your case, use the menu() function instead. I also wouldn't assign to ans as this can be overwritten way too easily.

Sean de Wolski
Sean de Wolski 2015년 3월 2일
You're passing in the string literal 'tod'. Since none of the cases in the switch-case tree have 'tod' as an option, cost never gets assigned and you get the error you're seeing.
You probably want to call the function like this:
calcCall(tod,doc);
Note the lack of single quotes around tod which indicates it's a variable not a string.
Another thing to do is when you have a switch case statement, I usually add an otherwise (i.e. if no cases were met). This makes debugging like this easier
otherwise
error('This shouldn''t happen')

Community Treasure Hunt

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

Start Hunting!

Translated by