using fprintf within a function

4 Ansichten (letzte 30 Tage)
steve guy
steve guy am 18 Feb. 2013
The function goes like this. How do I enter an fprintf statement so that the output looks like this:
>>num2let(83)
>>The corresponding letter grade to 83 is:B
function [grade]= num2let(n)
If n>89.49
grade='A'
elseif n<=89.49 & n=>79.5
grade='B'
......
...
elseif n<59.5 grade='F'
end
end
  1 Kommentar
Walter Roberson
Walter Roberson am 18 Feb. 2013
You have defined this as a function that returns a value. What do you want returned? The verbose string? Just the letter grade?
Remember, functions that return values cause the returned value to be printed out in the form
ans =
unless something is done with the returned value, or the display is suppressed by putting a semi-colon after the expression.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Youssef  Khmou
Youssef Khmou am 18 Feb. 2013
Bearbeitet: Youssef Khmou am 18 Feb. 2013
hi Caleb,
You can define your function without output like this :
function []= num2let(n)
if n > 89.49
fprintf('>>The corresponding letter grade to %d is:A\n',n)
elseif n<=89.49 && n>=79.5
fprintf('>>The corresponding letter grade to %d is:B\n',n)
% YOU COMPLETE YOUR FUNCTION WITH OTHER TESTS
elseif n<59.5
% grade='F'
fprintf('>>The corresponding letter grade to %d is:F\n',n)
end
end
  2 Kommentare
steve guy
steve guy am 18 Feb. 2013
thank you.
Jan
Jan am 18 Feb. 2013
When you have checked for n > 89.49 already, you do not need to check for elseif n <= 89.49 afterwards.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Thorsten
Thorsten am 18 Feb. 2013
If you do not insist on fprintf, you can get the output using
grade = 83;
disp(['The corresponding letter grade to ' int2str(grade) ' is ' ...
num2let(grade)])

Kategorien

Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by