file names with a shared beginning

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 comentarios

Rik
Rik el 24 de Mzo. de 2018
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 el 25 de Mzo. de 2018
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.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 25 de Mzo. de 2018
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 comentarios

Elaheh
Elaheh el 25 de Mzo. de 2018
Thank you. I am new to MATLAB. I'll try to do as suggested.
Why not try something simple like this:
folder = pwd;
dataFiles = [dir(fullfile(folder,'1*.ev2')); ...
dir(fullfile(folder,'2*.ev2'));...
dir(fullfile(folder,'3*.ev2'));...
dir(fullfile(folder,'4*.ev2'));...
dir(fullfile(folder,'5*.ev2'));...
dir(fullfile(folder,'6*.ev2'));...
dir(fullfile(folder,'7*.ev2'));...
dir(fullfile(folder,'8*.ev2'));...
dir(fullfile(folder,'9*.ev2'))]
If you want something more compact, someone will post a cryptic version that uses regexp(), I'm sure. But this works and is easy to understand what it does.
Elaheh
Elaheh el 25 de Mzo. de 2018
Thank you for this. It worked but the problem is I do not have fixed numbers at the begining (1,2,3,...). The first number is the same as the index I use in the for loop (p). It varies for each folder.
for p =1:length(directoryNames) folderName = directoryNames{p}; mgdFiles = dir(fullfile(folderName,'p_*.ev2 ')); end
If you just want one number at a time, then do this:
for p =1:length(directoryNames)
folderName = directoryNames{p};
filePattern = sprintf('%d_*.ev2', p);
mgdFiles = dir(fullfile(folderName, filePattern));
end
Elaheh
Elaheh el 25 de Mzo. de 2018
It is closer, but it still generates 1_*.ev2, which has * in the output (no file has visible * as part of the name).Just to remind that the files I have in each folder are,for example, 1_2_10.ev2, 1_1_13.ev2, 1111.ev2, 1113.ev2, 1_1_13.cnt, etc. I only need to pick up the .ev2 files with this common characters, 1_, where 1 should be p.
Image Analyst
Image Analyst el 25 de Mzo. de 2018
Of course it does. That's a wildcard character. It means it will match any character or group of characters. Did you try it? It will find all the files you mentioned.
Elaheh
Elaheh el 25 de Mzo. de 2018
Editada: Stephen23 el 25 de Mzo. de 2018
Each ev2 file begins with the folder number, eg for folder 3, the files start with 3_ etc. So, I think I should use folderName in the filePattern equation not p because p shows the file index not its name. For example, when p is 12, the fileName is 9, but when I put filename in the filepattern equation, it generates 57_*.ev2. as the filePattern?? Thank you so much.
For p =1:length(directoryNames) %folders (n=12)
folderName = directoryNames{p};
filePattern = sprintf('%d_*.ev2', folderName ); %files in each folder
mgdFiles = dir(fullfile(folderName, filePattern ));
for d = 1: length( mgdFiles)
mgdFile = strcat(folderName,'\', mgdFiles(d).name);
data1=dlmread(mgdFile);
end
end
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 el 25 de Mzo. de 2018
Thank you so much. It works

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Mzo. de 2018

Comentada:

el 25 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by