필터 지우기
필터 지우기

How to find selection frequency in a txt file?

조회 수: 3 (최근 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 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