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!