How to find selection frequency in a txt file?

1 回表示 (過去 30 日間)
MByk
MByk 2023 年 5 月 1 日
コメント済み: MByk 2023 年 5 月 2 日
Hello all, I have 10 txt files (I share an example) and each file has a list of selected items. I have 13 items in total. What I want to know is how many times each item was selected but I am getting an error (Input must be a MAT-file or an ASCII file containing numeric data with same number of columns in each row) message. I think the problem is the varying length of each line. How can I fix it? Thanks for the help.
txtFiles = dir('*.txt');
nFiles = length(txtFiles);
for i = 1:nFiles
txtCnt = load(txtFiles(i).name);
fprintf('-----%s-----\n',txtFiles(i).name);
Frq = zeros(1,13);
for Indx = 1:13
Frq(Indx) = sum(txtCnt == Indx);
end
Results = [1:13; Frq]
idx = find(Frq > mean(Frq))
end
Error using load
Unable to read file 'Testing.txt'. Input must be a MAT-file or an ASCII file containing numeric data with same number of columns in each row.

採用された回答

Cris LaPierre
Cris LaPierre 2023 年 5 月 1 日
Do not use load. I would probably use readmatrix. Note that your rows do not all have the same number of elements. These rows are padded with NaN.
data = readmatrix("Testing.txt")
data = 422×5
2 6 8 NaN NaN 2 6 8 13 NaN 2 6 8 12 NaN 6 8 NaN NaN NaN 6 8 12 NaN NaN 6 8 11 NaN NaN 6 8 13 NaN NaN 1 6 8 NaN NaN 1 6 8 13 NaN 3 6 8 12 NaN
  7 件のコメント
Cris LaPierre
Cris LaPierre 2023 年 5 月 2 日
Looks like MATLAB is trying to guess where the data starts. This is a result of having a different number of values in each row. Use the 'NumHeaderLines' name value pair to indicate that the data starts on the first line.
txtFiles = dir('*.txt');
nFiles = length(txtFiles);
for i = 1:nFiles
txtCnt = readmatrix(txtFiles(i).name,'NumHeaderLines',0)
fprintf('-----%s-----\n',txtFiles(i).name);
Frq = zeros(1,13);
for Indx = 1:13
Frq(Indx) = sum(txtCnt == Indx,'all');
end
Results = [1:13; Frq]
idx = find(Frq > mean(Frq))
end
txtCnt = 13×4
5 7 13 NaN 5 10 NaN NaN 1 5 7 NaN 1 5 7 10 1 5 10 NaN 1 5 13 NaN 7 10 13 NaN 10 13 NaN NaN 7 10 NaN NaN 1 7 13 NaN
-----Error.txt-----
Results = 2×13
1 2 3 4 5 6 7 8 9 10 11 12 13 8 0 0 0 6 0 7 0 0 8 0 0 6
idx = 1×5
1 5 7 10 13
MByk
MByk 2023 年 5 月 2 日
I spent hours trying to find hidden/special characters that could cause the error. :) I also tested xls format that resulted in same error. Thank you again.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by