selecting filenames from a dir list

I'm trying to create a subset of file names from a dir listing when many of the files have similar naming structures. for example I use the code:
dirname=pwd;
dirData=dir(dirname);
dirIndex=[dirData.isdir];
fileList={dirData(~dirIndex).name};
and I get a complete listing of the files.
Unfortunately, my files are named:
"Template_001.asc"
"Template_001_spent_fuel_tank1.asc"
"Template_001_spent_fuel_tank2.asc"
"Template_001_payload.asc"
"Template_002.asc" and so on through "Template_999_payload.asc"
What I need to do is create a matrix/array of just the files that have the base name of
"Template_001.asc"
"Template_002.asc"
"Template_003.asc"
...
"Template_999.asc"
from the populated fileList variable.
Any help would be greatly appreciated.

3 comentarios

per isakson
per isakson el 7 de Feb. de 2014
Editada: per isakson el 7 de Feb. de 2014
I have problems to exactly understand what "base name" means.
Woody
Woody el 7 de Feb. de 2014
Editada: Woody el 7 de Feb. de 2014
It's the subset of all of the files in the fileList variable that only contain "Template_001.asc" "Template_002.asc" "Template_003.asc" all the way to "Template_999.asc". How would I build a subset of just those files from the full listing of the files in fileList?
For example if fileList has: "Template_001.asc" "Template_001_spent_fuel_tank1.asc" "Template_001_spent_fuel_tank2.asc" "Template_001_payload.asc" "Template_002.asc" "Template_002_spent_fuel_tank1.asc" "Template_002_spent_fuel_tank2.asc" "Template_002_payload.asc" "Template_003.asc"
How would I build a list that only contains "Template_001.asc", "Template_002.asc" and "Template_003.asc"?
dpb
dpb el 7 de Feb. de 2014
Editada: dpb el 7 de Feb. de 2014
But if they're in the list you're returning from the previous dir() call, then with the proper wildcard matching they'll be the ONLY ones returned thus eliminating the need to do the selection after the fact.
Solve the problem by avoiding the problem in the first place.
Of course, if you're adamant of going at it the hard way, you can do pattern-matching on the content of the array similarly, but why make it more complex than needs be?

Iniciar sesión para comentar.

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 7 de Feb. de 2014
Editada: Azzi Abdelmalek el 7 de Feb. de 2014
A={'Template_001.asc'
'Template_001_spent_fuel_tank1.asc'
'Template_001_spent_fuel_tank2.asc'
'Template_001_payload.asc'
'Template_002.asc'}
out=A(~cellfun(@isempty,regexp(A,'.+(?<=_\d{3}\.asc)','match')))

3 comentarios

Woody
Woody el 7 de Feb. de 2014
Excellent! That regexp was where I was having trouble.
dpb
dpb el 7 de Feb. de 2014
But still, why not just select the ones desired to begin with????
Azzi Abdelmalek
Azzi Abdelmalek el 7 de Feb. de 2014
Editada: Azzi Abdelmalek el 7 de Feb. de 2014
Are you sure ??? works ?

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 7 de Feb. de 2014
Far simpler would be to simply return the ones you want...
d=dir([dirname\template_???.asc");
for i=1:length(d)
%process file d.name(i) here
...
Build an appropriate filter for whichever subset you want.

Categorías

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

Preguntada:

el 7 de Feb. de 2014

Editada:

el 7 de Feb. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by