How to read text files from different sub folders in a folder ?
Afficher commentaires plus anciens
I have a collection of .txt files which are an output of a simulation software.The software puts the text files in different sub-folders within a folder.Is it possible to read all of those ?
For example:- In a single folder named top, there are 500 sub-folders(each subfolder is of format TC_SYS_01,TC_SYS_02,.....TC_SYS_500) with a text-file in each of them.I want to read those text files from each of those folders & do some operations.
I'm thinking of changing the simulation software code itself so that it outputs txt files within a folder(no sub-folders) & use this code:
F = dir('*.txt');
for i = 1:length(F)
text = fileread(F(i).name) ;
% Do further operations
end
but can i do it without changing the simulation software code and this : http://in.mathworks.com/matlabcentral/newsreader/view_thread/301929 doesn't help me
Réponse acceptée
Plus de réponses (3)
Meade
le 2 Mar 2017
You can try the code below. It will do the same thing as Image Analyst's post, but is a little cleaner if you don't need any of the nested folder information, i.e. you only want the file paths.
mainFolder = uigetdir(); % Select your Main folder
[~,message,~] = fileattrib([mainFolder,'\*']);
fprintf('\n There are %i total files & folders.\n',numel(message));
allExts = cellfun(@(s) s(end-2:end),{message.Name},'uni',0);% Get file ext
TXTidx = ismember(allExts,'txt');% Search extensions for "CSV" at the end
TXT_filepaths = {message(TXTidx).Name}; % Use idx of TXTs to list paths.
fprintf('There are %i files with *.txt file ext.\n',numel(TXT_filepaths));
for ii = 1:numel(TXT_filepaths)
% Do import here
end
2 commentaires
StephMarine
le 16 Sep 2019
Do you know a way of using this if the file name has multiple . in it? the code runs well but reads the extension after the frist . so i have a few file names such as 000.111.222.csv so the ext is read as .111 any advice is appreciated!
"the code runs well but reads the extension after the frist "
There is nothing in this code that detects the period character, so it doesn't even know where the first period character is.
Note that the code is buggy as it incorrectly assumes that all file extensions have three characters: this can easily be fixed using fileparts.
"any advice is appreciated!"
Write simpler, more robust code using dir, fullfile, and fileparts.
KSSV
le 30 Sep 2015
0 votes
Hi
You can get the names of the sub folders inside a folder using the following piece of code.
dirinfo = dir(pathfolder);
dirinfo(~[dirinfo.isdir]) = []; %remove non-directories
dirinfo(1:2) = []; % Remove the first two fields as they are . and ..
dirinfo is a structure with all the information. You can run a loop along this structure, go to the specific folder and then you can select the text files in it.
Cheers
Srinivas
Thorsten
le 30 Sep 2015
Use getAllFiles from http://stackoverflow.com/questions/2652630/how-to-get-all-files-under-a-specific-directory-in-matlab/26449095#26449095
filenames = getAllFiles('.', '*.txt', 1);
and then loop through the filenames and work on them; you do not need to cd to the directories.
Catégories
En savoir plus sur File Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!