How to read files' names efficiently
2 次查看(过去 30 天)
显示 更早的评论
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 个)
Sean de Wolski
2014-7-15
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
2014-7-15
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 个评论
Alfonso Nieto-Castanon
2014-7-15
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!