Azzera filtri
Azzera filtri

How to access text files from many subfolders within a folder?

14 visualizzazioni (ultimi 30 giorni)
Hey! I want to access a main folder which has 44 subfolders. Each subfolder contains a txt file. I want to run a loop to open that text file and store its last line. Could you please help me with that? So far I have reached to this point:
if true
mainFolder = 'FEMA_P695_Far_Field_Long';
d = dir(mainFolder);
idxs = [d(:).isdir]';
names = {d(:).name}';
folders = names(idxs);
folders(ismember(folders,{'.','..'})) = [];
for k = 1:numel(folders)
myFolder = folders{1,1}(k);
cd folders{1,1}(k);
m = load('ida_curve.txt');
end end This is an incomplete code. From this I have a all my subfolders in the 'folders'. I dont know how to go ahead and open the text file within each subfolder and then how to write only the last line of the 'ida_curve.txt' file in a new file. Could you please help me with this? I am trying to learn new things in MATLAB.

Risposta accettata

Nikhil Negi
Nikhil Negi il 18 Mag 2018
Hello Vishvendra,
from what i can deduce from your description, you already have a list of folders you need. I will suggest you define a function that takes a file as input and extracts the last line of that file.
You can do that by defining the function that iterates through the text file and checks if the next line to current line is equal to '-1' this way you will know that the current line is the last line of that text file and return it. you should use fgetl to read a text file line by line
then you can loop over the list of folders you have and get the last line of all the files
fgetl documentation - https://www.mathworks.com/help/matlab/ref/fgetl.html

Più risposte (1)

Jan
Jan il 18 Mag 2018
Modificato: Jan il 18 Mag 2018
mainFolder = 'D:\FEMA_P695_Far_Field_Long'; % Use absolute paths
mainList = dir(mainFolder);
subFolder = {mainList([mainList.isdir]).name};
subFolder(ismember(subFolder, {'.','..'})) = [];
Result = cell(1, numel(subFolder));
for iSub = 1:numel(subFolder)
File = fullfile(mainFolder, subFolder{iSub}, 'ida_curve.txt');
Str = fileread(File);
CStr = strsplit(Str, '\n');
Result{iSub} = CStr{end};
end
Since Matlab R2016b, dir can work recursively:
mainFolder = 'D:\FEMA_P695_Far_Field_Long'; % Use absolute paths
fileList = dir(fullfile(mainFolder, '**', 'ida_curve.txt'));
Result = cell(1, numel(FileList));
for iFile = 1:numel(FileList)
File = fullfile(FileList(iFile).folder, FileList(iFile).name);
Str = fileread(File);
CStr = strsplit(Str, '\n');
Result{iSub} = CStr{end};
end
  2 Commenti
Vishvendra Bhanu
Vishvendra Bhanu il 20 Mag 2018
Modificato: Vishvendra Bhanu il 20 Mag 2018
Hey Jan! Thank you so much! I understand the code clearly and it is working almost perfectly fine.
Jan
Jan il 21 Mag 2018
Modificato: Jan il 21 Mag 2018
I'm glad that I could help you.
After some disputes in other threads, I want to mention, that it is a great benefit for the forum to avoid discussions about accepting of answers. :-)

Accedi per commentare.

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by