How can I display only the fprintf without the error shown and using "function"

1 view (last 30 days)
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".

Answers (1)

Image Analyst
Image Analyst on 29 Oct 2021
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

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!