필터 지우기
필터 지우기

Construct a filename and then open it

조회 수: 4 (최근 30일)
Zaida Escila Martínez Moreno
Zaida Escila Martínez Moreno 2018년 2월 21일
편집: Stephen23 2021년 4월 18일
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.

채택된 답변

Stephen23
Stephen23 2018년 2월 21일
편집: Stephen23 2021년 4월 18일
Using cd makes code slow and harder to debug: instead of using cd simply use faster and more reliable relative/absolute filenames. Try doing something like this:
D = 'MainFolder';
Ss = dir(fullfile(D,'Subject_*'));
Ss = natsortfiles(Ss([Ss.isdir])); % alphanumeric sort by folder name
for ks = 1:numel(Ss)
tmp = [Ss(Ks).name,'_*.txt'];
Sf = dir(fullfile(D,Ss(Ks).name,tmp));
Sf = natsortfiles(Sf); % alphanumeric sort by filename
for kf = 1:numel(Sf)
tmp = fullfile(D,Ss(ks).name,Sf(kf).name);
table = readtable(tmp);
...
end
end
To get the filenames in the correct order I used my FEX submission natsortfiles, which provides an alphanumeric sort of filenames. To use it you will need to download it, unzip it, and place it on the MATLAB Search Path (e.g. in the current directory):

추가 답변 (2개)

KSSV
KSSV 2018년 2월 21일
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
Zaida Escila Martínez Moreno
Zaida Escila Martínez Moreno 2018년 2월 21일
The reason why I think I cannot do that is because I have more .txt than the ones that I want to read. And they are not ordered in the order I want to use them that is from 1 to 10. Is there any other way of doing it? Thank you!

댓글을 달려면 로그인하십시오.


Zaida Escila Martínez Moreno
Zaida Escila Martínez Moreno 2018년 2월 21일
So I finally was able to solve that part of what I am trying to do and I did it through this way. This can be used in two loops, the first one with kk iterating would change the folder and the second one with jj iterating would change the file that is opened in each loop.
cd 'MainFolder';
FolderList = dir('Subject*');
Subject = FolderList(kk,1).name;
cd(Subject);
fileroot = strcat(Subject,'_',num2str(jj));
filename = sprintf('%s_*.txt',filename);
FilesList = dir(filename);
table = readtable(FilesList);
:)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by