How to read files' names efficiently

1 view (last 30 days)
Haimon
Haimon on 15 Jul 2014
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.

Answers (2)

Sean de Wolski
Sean de Wolski on 15 Jul 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
  1 Comment
Haimon
Haimon on 15 Jul 2014
Sorry, I think I did not explain it very well.
The thing is, that I have 2 datasets, e.g., frogA0001 up to frogA2000 and frogB0001 up to frogB2000; or rock, or tooth, or whatever.
What I really wanted would be that MatLab not only finds the pattern, like frogA0000, and then reads all 2000 images, one at a time of course, but also can distinguish between the frogA0000 and frogB0000.
As of know, my code needs the start, which would be the "frogA0000" and "frogB0000", which is a waste of time, since every time I have samples that have different names.

Sign in to comment.


Alfonso Nieto-Castanon
Alfonso Nieto-Castanon on 15 Jul 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 Comments
Alfonso Nieto-Castanon
Alfonso Nieto-Castanon on 15 Jul 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).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by