extract data from a specific line of a txt file
33 次查看(过去 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!