How to find selection frequency in a txt file?

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by