How to read files' names efficiently
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I work with tomographic images. Usually I have between a thousand or 2 thousand images to work with.
My data usually has different names. Could go from "tooth", "frog", "rock", etc. And it usually is numbered from 0 to almost 2000.
So an example would be:
"rock0001", "rock0002", ...., "rock1999", "rock2000"
When I started working with MatLab, I found out a way to read the files names, so I can access the data from matlab. However I have to start the pattern first. I have to give to matlab the "rock0000", or "frog0000", so it knows what is the name and how much letters and numbers it has.
What I wanted to do is something more efficiently. I wanted to write this initial part of the code in a way that matlab finds the pattern and the length itself, so I do not need to start the name of the file anymore, since it changes for every sample.
Does anyone knows how to do that? Thanks for any help you can give.
2 comentarios
Respuestas (2)
Sean de Wolski
el 15 de Jul. de 2014
You can use the dir command with a wildcard (*) to do this for you.
dir('.\*.png')
Or
dir('.\frog*')
etc..
More info at:
doc dir
Alfonso Nieto-Castanon
el 15 de Jul. de 2014
Something like:
files = dir; % Your list of files
[~,filenames] = cellfun(@fileparts,{files.name},'uni',0); % Your list of filenames
pattern = unique(regexprep(filenames, '^(\D+)(\d+)$','$1${repmat(''0'',1, numel($2))')); % the list of unique word+number patterns
3 comentarios
Alfonso Nieto-Castanon
el 15 de Jul. de 2014
I am assuming you have code for that but you are now manually entering into your code something like:
pattern = 'frog0000';
The code that I sent finds these patterns from the dir output (assuming your folder only contains these sort of files) so you just need to run your existing code using each element of the cellarray pattern (one for each different pattern found).
Ver también
Categorías
Más información sobre Data Import and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!