How to read files' names efficiently

조회 수: 1 (최근 30일)
Haimon
Haimon 2014년 7월 15일
댓글: Alfonso Nieto-Castanon 2014년 7월 15일
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
Azzi Abdelmalek
Azzi Abdelmalek 2014년 7월 15일
This is not clear
Haimon
Haimon 2014년 7월 15일
Why not?

댓글을 달려면 로그인하십시오.

답변 (2개)

Sean de Wolski
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
  댓글 수: 1
Haimon
Haimon 2014년 7월 15일
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.

댓글을 달려면 로그인하십시오.


Alfonso Nieto-Castanon
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
Haimon
Haimon 2014년 7월 15일
Hi Alfonso,
I'm not gonna lie, I didn't understand your "code".
For example, how would the program know the extension using your code? .bmp? .tiff?
How would I be able to access the name of the images and them use those informations afterwards?
For example, if I have 2000 images, how can I access one image at a time?
Alfonso Nieto-Castanon
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 CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by