ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ
ํ•„ํ„ฐ ์ง€์šฐ๊ธฐ

Display or print char data

์กฐํšŒ ์ˆ˜: 52 (์ตœ๊ทผ 30์ผ)
Adham Ayman
Adham Ayman 2020๋…„ 7์›” 18์ผ
๋‹ต๋ณ€: Image Analyst 2020๋…„ 7์›” 18์ผ
x = input(โ€˜Enter the grades: โ€™)
For i=1:length(x)
if x(i) >= 90
x(i)= 'A';
elseif x(i) >= 80
x(i) = 'B';
elseif x(i) >= 70
x(i) = 'C';
elseif x(i) >= 60
x(i) = 'D';
else
x(i) = 'F';
end
end
disp(x)
  • Input:[10 12 90 60 47 78]
  • output:[70 70 65 68 70 67]
  • # | When I turn on this code, it prints numbers instead of letters on the screen and gives me a warning message that the argument must be of type of char, is there an answer please? (Sorry, the first code wasnโ€™t the desired)๐Ÿ˜…
  ๋Œ“๊ธ€ ์ˆ˜: 2
KSSV
KSSV 2020๋…„ 7์›” 18์ผ
The code looks fine.....it prints the grade as string..
Walter Roberson
Walter Roberson 2020๋…„ 7์›” 18์ผ
The code works for me... at least after I removed the _ that you accidentally had at the end of the line.

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.

๋‹ต๋ณ€ (2๊ฐœ)

madhan ravi
madhan ravi 2020๋…„ 7์›” 18์ผ
clear all
It worked for me after removing _ at the very end
  ๋Œ“๊ธ€ ์ˆ˜: 4
madhan ravi
madhan ravi 2020๋…„ 7์›” 18์ผ
Adham Ayman
Adham Ayman 2020๋…„ 7์›” 18์ผ
ok, thank you for great help :)

๋Œ“๊ธ€์„ ๋‹ฌ๋ ค๋ฉด ๋กœ๊ทธ์ธํ•˜์‹ญ์‹œ์˜ค.


Image Analyst
Image Analyst 2020๋…„ 7์›” 18์ผ
I would use fprintf() instead of disp(). I also wouldn't use the same variable to be a double, and a string, at different times. I think that's bad practice. Here is one way of doing it:
numberGrades = input('Enter the grades (inside brackets) : ')
for k = 1 : length(numberGrades)
if numberGrades(k) >= 90
letterGrade{k} = 'A';
elseif numberGrades(k) >= 80
letterGrade{k} = 'B';
elseif numberGrades(k) >= 70
letterGrade{k} = 'C';
elseif numberGrades(k) >= 60
letterGrade{k} = 'D';
else
letterGrade{k} = 'F';
end
fprintf('A score of %d is an "%s".\n', numberGrades(k), letterGrade{k});
end
numberGrades =
40 88
A score of 40 is an "F".
A score of 88 is an "B".
Another way would be to use a table with numbers in the first column and letters in the second column.
numberGrades = input('Enter the grades (inside brackets) : ')
grades = table(...
'Size', [length(numberGrades), 2],...
'VariableNames', {'NumberGrade', 'LetterGrade'},...
'VariableTypes', {'double', 'string'});
grades{:, 1} = numberGrades'; % Put number grades into column 1.
for k = 1 : length(numberGrades)
if numberGrades(k) >= 90
grades{k, 2} = 'A';
elseif numberGrades(k) >= 80
grades{k, 2} = 'B';
elseif numberGrades(k) >= 70
grades{k, 2} = 'C';
elseif numberGrades(k) >= 60
grades{k, 2} = 'D';
else
grades{k, 2} = 'F';
end
fprintf('A score of %d is an "%s".\n', grades{k, 1}, grades{k, 2});
end
grades
Enter the grades (inside brackets) : [85,77,96]
numberGrades =
85 77 96
A score of 85 is an "B".
A score of 77 is an "C".
A score of 96 is an "A".
grades =
3ร—2 table
NumberGrade LetterGrade
___________ ___________
85 "B"
77 "C"
96 "A"

์นดํ…Œ๊ณ ๋ฆฌ

Help Center ๋ฐ File Exchange์—์„œ Data Import and Analysis์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ

ํƒœ๊ทธ

Community Treasure Hunt

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

Start Hunting!

Translated by