The code works but the 1st column shows NaN and 3 numeric data is missing.

2 vues (au cours des 30 derniers jours)
Allen Lee
Allen Lee le 15 Mar 2022
Commenté : Arif Hoq le 15 Mar 2022
I am coding a program that will conclude 3 different classes and group the grades in 10 group. I using readmatrix function and turns up the 1st column of the text file will become NaN and 3 numeric data was missing. How to get throught it??
clear;
clc;
data = readmatrix('hw3.txt');
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
g = 0;
h = 0;
j = 0;
k = 0;
for i = 1:45
if data(i) >=0 && data(i) <= 9
a = a + 1;
elseif data(i) >= 10 && data(i) <= 19
b = b + 1;
elseif data(i) >= 20 && data(i) <= 29
c = c + 1;
elseif data(i) >= 30 && data(i) <= 39
d = d + 1;
elseif data(i) >= 40 && data(i) <= 49
e = e + 1;
elseif data(i) >= 50 && data(i) <= 59
f = f + 1;
elseif data(i) >= 60 && data(i) <= 69
g = g + 1;
elseif data(i) >= 70 && data(i) <= 79
h = h + 1;
elseif data(i) >= 80 && data(i) <= 89
j = j + 1;
elseif data(i) >= 90 && data(i) <= 100
k = k + 1;
end
end
fprintf('0~9: %d\n', a);
fprintf('10~19: %d\n', b);
fprintf('20~29: %d\n', c);
fprintf('30~39: %d\n', d);
fprintf('40~49: %d\n', e);
fprintf('50~59: %d\n', f);
fprintf('60~69: %d\n', g);
fprintf('70~79: %d\n', h);
fprintf('80~89: %d\n', j);
fprintf('90~100: %d\n', k);

Réponses (1)

Arif Hoq
Arif Hoq le 15 Mar 2022
Modifié(e) : Arif Hoq le 15 Mar 2022
try this:
data = readtable('hw3.txt','ReadVariableName',false,'delimiter',':');
data2=table2array(ata(:,2))
data3=split(data3,',')
  2 commentaires
Arif Hoq
Arif Hoq le 15 Mar 2022
thy this code:
B = readtable('hw3.txt','ReadVariableName',false,'delimiter',':');
data2=B(:,2);
data3=table2array(B(:,2));
data4=str2double(split(data3,','));
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
g = 0;
h = 0;
j = 0;
k = 0;
for i = 1:45
if data4(i) >=0 && data4(i) <= 9
data4 = data4 + 1;
elseif data4(i) >= 10 && data4(i) <= 19
b = b + 1;
elseif data4(i) >= 20 && data4(i) <= 29
c = c + 1;
elseif data4(i) >= 30 && data4(i) <= 39
d = d + 1;
elseif data4(i) >= 40 && data4(i) <= 49
e = e + 1;
elseif data4(i) >= 50 && data4(i) <= 59
f = f + 1;
elseif data4(i) >= 60 && data4(i) <= 69
g = g + 1;
elseif data4(i) >= 70 && data4(i) <= 79
h = h + 1;
elseif data4(i) >= 80 && data4(i) <= 89
j = j + 1;
elseif data4(i) >= 90 && data4(i) <= 100
k = k + 1;
end
end
fprintf('0~9: %d\n', a);
fprintf('10~19: %d\n', b);
fprintf('20~29: %d\n', c);
fprintf('30~39: %d\n', d);
fprintf('40~49: %d\n', e);
fprintf('50~59: %d\n', f);
fprintf('60~69: %d\n', g);
fprintf('70~79: %d\n', h);
fprintf('80~89: %d\n', j);
fprintf('90~100: %d\n', k);
Arif Hoq
Arif Hoq le 15 Mar 2022
@Allen Lee please accept my answer if you found it useful

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by