How to have a user select a file and then matlab automatically go into multiple subfolders and pull out a multiple text files and import them in as a table

4 views (last 30 days)
I am trying to have a user select a folder from a directory which I am using the uigetdir function. After the user selects a file there is are mutiple subfolders labeled with trial numbers. Within the each trial folder is anotehr subfolder "Data" that I want matlab to go into and pull a text file labled "imudata.txt" and bring it into the workspace as a table.
the file structure is like this
  • pt_01 folder
  • - Trial 1
  • -> Data
  • imudata.txt
  • - Trial 2
  • -> Data
  • imudata.tx
  • - Trial 3
  • -> Data
  • imudata.txt
  • So I want the user to 1) select a folder
  • and then matlab automatically go into each trial folder-> and into the subfolders named "data"-> and pull all the "imudat.txt" files and bring them in as a seperate tables
I am thinking of doing this first but not sure how to have matlab go into each trial folder and subfolder and pull the specific files
pt_selction = uigetdir('C:\user\project1\') %allows user to select the folder of interest
Trials = dir(pt_selection)
Subfold = Trials([Trials.isdir])
%Loop on each folder
Subfold = [];
for i=1:length(Subfold)
filetoread = fullfile(pt_selection,SubFold(i).name,'imudata.txt')

Accepted Answer

Stephen23 on 9 Feb 2024
Edited: Stephen23 on 9 Feb 2024
Let DIR to do the work for you! DIR will happily search in subfolders for specific files, if you tell it to:
P = uigetdir('C:\user\project1\');
F = fullfile(P,'Trial*','Data','imudata.txt');
S = dir(F);
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
T = readtable(F); % pick a suitable file importing function and options.
S(k).data = T;
All of the data is simply stored in the structure S. For example, the 2nd file:
S(2).folder % location
S(2).name % filename
S(2).data % imported data

More Answers (1)

Sonesson on 9 Feb 2024
Hello Isabelle,
The wildcard * combined with dir which lists all contents in a folder is a powerful tool for this job i suggest something like this:
%Select a folder from pop up (starts pop up in current directory, cd)
myFolder = uigetdir(cd); %Select pt_01 folder here
%In myFolder, look for all folders. Look for the Data folder in those, and
%look for the imudata.txt in the data folders found
textFiles = dir([myFolder,'\**\Data\imudata.txt']);
%Loop over all such files found
for i = 1:size(textFiles,1)
%Read the text file as a table
t = readtable([textFiles(i).folder,'\',textFiles(i).name]);
%Make a suitable variable name for the table ( could be done with
%sprintf instead if you don't want to base it on the folder names)
%split the folder path into components
name = strsplit(textFiles(i).folder,'\');
%Take the second to last (Trial i) folder as the name
name = name{end-1};
%... But matlab cannot have variable names with spaces so we replace
name = strrep(name,' ','_');
%Lets also make the name point out that it is a table with data (highly optional)
name = [name,'_tableData'];
%Make the table as a seperate variable in the workspace
%... you can also assign it in the caller worspace
% instead of base if you want to format this as a function
Though be careful using assignin, it is typically bad practice as you may create variables with varying names, making them hard to reference later in the code!
Good luck!
  1 Comment
Stephen23 on 9 Feb 2024
Note that a recursive directory search is not required.
Using FULLFILE is much better than hard-coding path separator characters with text concatenation.
Using ASSIGNIN with variable names based on filenames is fundamentally fragile and best avoided:

Sign in to comment.


Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!