Load text file from subfolder
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sohel Rana
el 9 de Sept. de 2020
Comentada: Walter Roberson
el 14 de Sept. de 2020
Hello Creative People,
I'm facing problems in loading data from subfolders. There is a main folder called "fiber optics" and this folder contains about 200k subfolders. Each subfolder contains just only one text file. How can I load text file from each subfolder one after another and plot it?
0 comentarios
Respuesta aceptada
Walter Roberson
el 9 de Sept. de 2020
Editada: Walter Roberson
el 9 de Sept. de 2020
projectdir = 'fiber optics';
dinfo = dir(fullfile(projectdir, '**', '*.txt'));
filenames = fullfile({dinfo.folder}, {dinfo.name});
nfiles = length(filenames);
datum = cell(nfiles,1);
foldernames = cell(nfiles,1);
for K = 1 : nfiles
thisfile = filenames{K};
fullparent = fileparts(thisfile);
[~, foldername] = fileparts(fullparent);
datum{K} = load(thisfile);
foldernames{K} = foldername;
end
Now you have a cell array, datum, and a cell array foldernames, and can plot as appropriate, using the foldernames as labels.
I doubt that you want 200k lines on one plot, so I will not try to guess how you want to do the plotting.
13 comentarios
Walter Roberson
el 14 de Sept. de 2020
title(labels{K})
to have it show up in the title.
Use labels{K} as the 'DisplayName' option on whatever plot() you do use, if instead you want the name to show up when you
legend show
Más respuestas (1)
Adam Wyatt
el 9 de Sept. de 2020
BaseDir = 'BaseDir'; % Change string to base folder
tmp = dir(BaseDir); % Obtains list of files and folders in BaseDir
SubDir = {tmp.name}; % Extract list of names
SubDir = SubDir([tmp.isdir]); % Select entries that are directories
SubDir(1:2) = []; % Ignore first two entries ('.' and '..')
Data = cell(size(SubDir)); % Preallocate data cell array
% Loop through subfolders and perform custom load function, saving output to cell
for n=1:length(SubDir)
FullDir = fullfile(BaseDir, SubDir); % Create full folder name
fnames = dir(fullfile(FullDir, '*.txt')); % Obtain filename (assuming 'txt' extension)
Data{n} = LOAD_FUNCTION(fullfile(FullDir, fname(1).name)); % Either change LOAD_FUNCTION to built in text read function or your custom function that simply takes the filename as input
end
The concept is to first obtain the list of subfolders, then to loop through each subfolder and find the filename before loading it.
If the filename is the same and known in advance, you can simply use that.
If there are multiple files in the folder, you can loop through each file.
If the output is always the same size and format, you can output to a pre-allocated array rather than a cell array - I've assumed the worst-case scenario of different outputs from each file.
You need to change the BaseDir folder and the LOAD_FUNCTION function name as appropriate.
0 comentarios
Ver también
Categorías
Más información sobre View and Analyze Simulation Results en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!