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

2 ビュー (過去 30 日間)
Allen Lee
Allen Lee 2022 年 3 月 15 日
コメント済み: Arif Hoq 2022 年 3 月 15 日
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);

回答 (1 件)

Arif Hoq
Arif Hoq 2022 年 3 月 15 日
編集済み: Arif Hoq 2022 年 3 月 15 日
try this:
data = readtable('hw3.txt','ReadVariableName',false,'delimiter',':');
data2=table2array(ata(:,2))
data3=split(data3,',')
  2 件のコメント
Arif Hoq
Arif Hoq 2022 年 3 月 15 日
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 2022 年 3 月 15 日
@Allen Lee please accept my answer if you found it useful

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by