extract data from a specific line of a txt file
18 views (last 30 days)
Show older comments
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 Comments
Jan
on 8 Nov 2022
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?
Accepted Answer
Jan
on 9 Nov 2022
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 Comments
More Answers (1)
Marcel
on 9 Nov 2022
Edited: Marcel
on 9 Nov 2022
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 Comments
Jan
on 9 Nov 2022
Edited: Jan
on 9 Nov 2022
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.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!