How can I display only the fprintf without the error shown and using "function"
1 次查看(过去 30 天)
显示 更早的评论
function [category] = iqcategory (score)
%by Manav Divekar, 2021-10-28
%if the given score is greater the 130
if score >= 130
fprintf ('Very Superior \n');
elseif score >= 120 && score < 130
fprintf ('Superior \n');
elseif score >= 109 && score < 120
fprintf ('High Average \n');
else
fprintf ('Average \n');
end
>> disp( iqcategory(112.5) )
High Average
error Output argument "category" (and maybe others) not assigned during call to "iqcategory".
0 个评论
回答(1 个)
Image Analyst
2021-10-29
The error is because you said that your function would return category but you never set a value for it, so it's undefined when it comes time to exit your function. So you either have to set it so something (even null if you want):
function category = iqcategory (score)
category = []; % Initialize to null.
%by Manav Divekar, 2021-10-28
%if the given score is greater the 130
if score >= 130
fprintf ('A score of %.1f is Very Superior.\n', score);
category = 'A score of %.1f is Very Superior';
elseif score >= 120 && score < 130
fprintf ('A score of %.1f is Superior.\n', score);
category = 'Superior';
elseif score >= 109 && score < 120
fprintf ('A score of %.1f is High Average.\n', score);
category = 'High Average';
else
fprintf ('A score of %.1f is Average.\n', score);
category = 'Average';
end
or else just don't bother returning it at all, which means of course you don't set it, or return it, and consequently you won't get the error.
function iqcategory (score)
%by Manav Divekar, 2021-10-28
%if the given score is greater the 130
if score >= 130
fprintf ('A score of %.1f is Very Superior.\n', score);
elseif score >= 120 && score < 130
fprintf ('A score of %.1f is Superior.\n', score);
elseif score >= 109 && score < 120
fprintf ('A score of %.1f is High Average.\n', score);
else
fprintf ('A score of %.1f is Average.\n', score);
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!