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에서 Get Started with MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!