필터 지우기
필터 지우기

file names with a shared beginning

조회 수: 7 (최근 30일)
Elaheh
Elaheh 2018년 3월 24일
댓글: Elaheh 2018년 3월 25일
I have different files in a folder, but I want to select files such as 1_1_11.ev2, 1_2_13.ev2, with this common part "strcat(num2str(p),'_')". What should I write instead of the blank in the 3rd line after the "folderName"?
for p =1:length(directoryNames)
folderName = directoryNames{p};
Files = dir(strcat(folderName, ... ));
end
  댓글 수: 2
Rik
Rik 2018년 3월 24일
Does something like dir(strcat(folderName,'*.ev2')); work for you? If not, you can always use a regular expression to find the file names that fit your pattern.
Image Analyst
Image Analyst 2018년 3월 25일
Elaheh's "Answer" moved here since it's a comment to Rik rather than an answer to the posted question.
Thank you for your comment. The point is that there are files with .ev2, which I do not need. I need only the files that have strcat(num2str(p),'_') at the beginning in common. 1_1_13.ev2, 1_2_18.ev2. etc.

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

채택된 답변

Image Analyst
Image Analyst 2018년 3월 25일
You could try something like this:
for p =1:length(directoryNames)
folderName = directoryNames{p};
filePattern = fullfile(folderName, '*.ev2');
for k = 1 : length(filePattern)
thisFileName = filePattern(k).name;
if UseThisFile(thisFileName)
% Do something with it.
end
end
end
You would write a function called UseThisFile() that determines if the file is one you want to use or not based on the characters between the underlines. So that function would parse the filename and determine if it's OK to use or not and return true or false, respectively.
  댓글 수: 9
Image Analyst
Image Analyst 2018년 3월 25일
Then use %s instead of %d. Try this:
for p =1:length(directoryNames) %folders (n=12)
folderName = directoryNames{p};
filePattern = sprintf('%s_*.ev2', folderName); % files in each folder
mgdFiles = dir(fullfile(folderName, filePattern));
for d = 1: length(mgdFiles)
mgdFile = fullfile(folderName, mgdFiles(d).name);
data1 = dlmread(mgdFile);
end
end
Elaheh
Elaheh 2018년 3월 25일
Thank you so much. It works

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by