Indexing in nonsequential order

조회 수: 2 (최근 30일)
julian gaviria
julian gaviria 2025년 1월 21일
편집: Stephen23 2025년 1월 21일
The following code copy files from the source folder to a destination folder:
SourcePath ='/f1/f2/f3/f4/f5/f6/f7/f8/';
index = [00001]
%iteration for the 9th subfolder
for i = 1 : length(index)
newParticipant=num2str(index(i));
T = ['sub-m',newParticipant];
SourceFolder = fullfile([SourcePath,T],'f10/f11')
%Destination folder is the current directory + DistFolder
DistFolder = fullfile('d1',T,'d3');
[X,Y,Z] = copyfile(fullfile(SourceFolder,'*y_file.chik.chik'), ...
DistFolder);
end
Question: How can I automate the index when the subbfolder's name (see above 9th subfolder) are 5-digits non sequential numbers?. The codeline
index = 00001 : 02500;
yileds an undesired output: It will create multiple empty folders in "distfolder".
For instance, the first file to copy is located in
SourceFolder = '/f1/f2/f3/f4/f5/f6/f7/f8/sub-m00001/f10/f11'
The Line index = 00001 : 02500; will create 120 empty files, since the second file to copy is located in
SourceFolder = '/f1/f2/f3/f4/f5/f6/f7/f8/sub-m00121/f10/f11'
  댓글 수: 2
Stephen23
Stephen23 2025년 1월 21일
편집: Stephen23 2025년 1월 21일
Why not simply use DIR() ?
Perhaps something like this:
S = dir('/f1/f2/f3/f4/f5/f6/f7/f8/sub-m*/f10/f11/*')
Star Strider
Star Strider 2025년 1월 21일
My point exactly!

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

답변 (2개)

Rik
Rik 2025년 1월 21일
Your code is so confusing that I don't know what edit to suggest, but I expect you need to use the exist function to check whether a file exists before you create a new folder.

Star Strider
Star Strider 2025년 1월 21일
I do not completely understand your problem, however one solution could be to use the dir function to retrieve all the files in the original directory, and then extract the numbers from that list into a vector. There are several ways to do that (some obvious and others less so), depending on the original file names.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by