필터 지우기
필터 지우기

How to address files that are starting with certain word

조회 수: 69 (최근 30일)
Homayoon
Homayoon 2016년 3월 7일
댓글: Jan 2016년 3월 8일
Dear All,
In my main directory I do have 5 subdirectories(named 1,2,3,4 and 5) in all of which there is only one file whose name starts with 'word.' . (After . there is a 5-digit number)
I have written a code on the parent directory which goes into each subdirectories in the order. In each subdirectory I need to fopen the file whose name starts with 'word.', what should I do? Since I do not have any idea about the 5 digit number coming with the name of the file, I cannot use sprintf. Do you have any idea how I can do it?
If the 5-digit number was known then it was really simple as:
input = sprintf('%s%d','REAX.o',12345);
fid = fopen (input, 'r');
But now it is confusing to me.
Thank you so much

답변 (1개)

Jan
Jan 2016년 3월 7일
편집: Jan 2016년 3월 7일
FileList = dir(fullfile(Folder, 'word.*'));
FileName = fullfile(folder, FileList(1).name));
fid = fopen(FileName);
...
You will find a lot of submissions in the FileExchange, which find files recursively in subfolders.
  댓글 수: 10
Homayoon
Homayoon 2016년 3월 7일
I figured it out and this was something that I was looking for: (I had to use sprintf for some reasons)
foldername = sprintf('C:\\Users\\h\\h\\Tt\\Actual Analysis\\BI\\reverse-input\\New folder\\9\\%d',C(1,k));
cd(foldername);
filelist = dir(fullfile( 'REAX.o*'));
filelist(1).name;
out = sprintf ('C:\\Users\\h\\h\\Tt\\Actual Analysis\\BI\\reverse-input\\New folder\\9\\%d\\%s', C(1,k) ,filelist(1).name);
fid = fopen(out,'rt');
Jan
Jan 2016년 3월 8일
Sorry, Homayoon, this is a blind guessing.
  • You do not have to insert double slashes only to remove them using sprintf:
foldername = fullfile( ...
'C:\Users\h\h\Tt\Actual Analysis\BI\reverse-input\New folder\9\', ...
sprintf(%d', C(1,k)));
filelist = dir(fullfile(foldername, 'REAX.o*'));
out = fullfile(foldername, filelist(1).name);
  • Omit the "cd(foldername);", but use absolute file names only.
  • Omit the useless line "filelist(1).name;"
And now this is equivalent to the code I've posted. Voilà.

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

카테고리

Help CenterFile Exchange에서 Audio and Video Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by