Construct a filename and then open it
Show older comments
Hello! I am new at Matlab and I have been struggling to find the answer of this question, so please help me. I have a series of .txt (Subject_01_1_..., Subject_01_2_...) that are in different folders (Subject_01, Subject_02...). Each .txt file contains data that I want to import to MatLab and then use. I don't know if my code is the right way of doing it but with it, I would like to import through readtable each specific txt file to an specific column of a cell array. In this way, it would be like this: {'Subject_01_1...', 'Subject_01_2...', ...}. I am wondering how I can construct the name of the filename from the FolderList in my code (first part of the name) and then a '_', and then the number jj of the second loop.
Here is the beginning of my code:
for kk = 1:20
for jj = 1:10
cd 'MainFolder'
FolderList = dir('Subject*');
cd(FolderList(kk,1).name);
My folders look like this:
MainFolder
>Subject_01
>Subject_01_1....txt
>Subject_01_10....txt
>Subject_01_2....txt
>Subject_01_3....txt
...
>Subject_02
>Subject_02_1....txt
>Subject_02_10....txt
...
>Subject_03
...
Thank you so much and if there is an easier way of doing it, please let me know. Thank you! Zaida.
Accepted Answer
More Answers (2)
KSSV
on 21 Feb 2018
files = dir('*.txt') ; % you are in the present folder where text files are present
N = length(files) ;
for i = 1:N
filename = files(i).name
% do waht you want
end
1 Comment
Zaida Escila Martínez Moreno
on 21 Feb 2018
Zaida Escila Martínez Moreno
on 21 Feb 2018
Categories
Find more on MATLAB 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!