extract data from a specific line of a txt file

36 vues (au cours des 30 derniers jours)
Tim Dreßler
Tim Dreßler le 8 Nov 2022
Modifié(e) : Marcel le 11 Nov 2022
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 commentaires
Jan
Jan le 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?
Tim Dreßler
Tim Dreßler le 8 Nov 2022
Thank you for your fast answer. Yes, the files are stored in one folder. I will try to figure something out and get back if it does not work. Thanks!

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 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'));

Plus de réponses (1)

Marcel
Marcel le 9 Nov 2022
Modifié(e) : Marcel le 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 commentaires
Jan
Jan le 9 Nov 2022
Modifié(e) : Jan le 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.
Marcel
Marcel le 11 Nov 2022
Modifié(e) : Marcel le 11 Nov 2022
but it works and im not a performence shark so i dont really care :P . If it'd be that important for me to have the fastest code i'd go with c++ or similar languages

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by