extract data from a specific line of a txt file
조회 수: 30 (최근 30일)
이전 댓글 표시
I have the following problem: I have multiple (a lot of) .txt files which contain numbers as well as text. Within each .txt file there is one line from which I shows the word "good" oder "bad" (according to the data depicted in the file). The line isn't the same for each file. I would like to do the following: 1. I want to add a line to the textfile in which the name of the file is represented. 2. I want to extract a table (or whatever datatype) which shows the name of the file in one collomn, and the word ("good" or "bad") in another collomn (the different files should be the lines). All of this should be automated. I hope this wasn't to confusing. Thanks for your help. btw: I didn't code anything yet because I wanted to wait if there is a smart solution to the problem.
댓글 수: 2
Jan
2022년 11월 8일
Start with coding it and post at least the rought structure. This is more efficient, than letting the readers do all the work.
"I want to add a line to the textfile in which the name of the file is represented" - where? What about appening it at the end?
"I want to extract a table (or whatever datatype) which shows the name of the file in one collomn, and the word ("good" or "bad") in another collomn" - From where do you want to extract such a table? Do you mean, that you want to create such a table?
How can the list of files be obtained? Are they stored in the same folder?
채택된 답변
Jan
2022년 11월 9일
Using modern functions to parse the file:
Lines = readlines('path/to/file.txt');
Lines = strtrim(Lines); % Maybe some leading or trailing spaces
isGood = any(matches(Lines, 'good'));
isBad = ~isGood && any(matches(Lines, 'bad'));
댓글 수: 0
추가 답변 (1개)
Marcel
2022년 11월 9일
편집: Marcel
2022년 11월 9일
Maybe you can adobt the code i once made on github.
I think you'd need to do something like this to find the words from the text file
fid = fileread("path/to/file"); % read the file
filecontent_split = regexp(fid,'\n','split'); % split by new line
% Will indicate where the line was found
lineFound = 0;
for i = 1:length(filecontent_split)
% Line found!
if filecontent_split(i) == "good" | filecontent_split(i) == "bad"
% index of the line where the word was found
lineFound = i;
end
end
If you want to do this for each file in the directory, you might wanna use something like this as well
% Target Folder
D = string(ls("path/of/dir"));
D = strrep(D, " ", ""); % We dont need them so we get rid of them
D = strrep(D, "..", "");
% For each folder...
for i=1:length(D)
if D(i) ~= "" & D(i) ~= "."
disp("Found file " + D(i));
end
end
fclose('all'); % maybe not the best to close "all", just an example
댓글 수: 4
Jan
2022년 11월 9일
편집: Jan
2022년 11월 9일
D = string(ls("path/of/dir")); is rather indirect: It calls dir to create a list of files, prints them as CHAR vector. Then it is converted to a strings, which is spülit to get the file names again. The direct approach:
D = dir('path/of/dir');
FileName = {D.name};
FileName(ismember(FileName, {'.', '..'})) = [];
for k = 1:numnel(FileName)
...
end
The line
fid = fileread("path/to/file");
uses a confusing name of the output. This is not a file identiifier, but the contents of the file.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!