Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
separate filename characters and numbers

Subject: separate filename characters and numbers

From: Rajesh

Date: 18 Mar, 2010 07:00:05

Message: 1 of 11

Hi,

I have a directory with multiple tif images named as say 'file1','file2' ...file100 and so on.
To begin with i will not be knowing what the exact name is like e.g. the names may also be like image1, image2.... or file_001, file_002 etc.

Is there a way to parse the file name by say picking a file from the directory and breaking the name into characters and numbers and then use a for loop to open the files one after another?

There may be other files in the directory other than the series of files that i am interested in and i dont want to read / open them.

I saw strtok,strcmp,strfind,regexp but as i understood it requires a character or a set of characters to compare the entire string.

thanks..

Subject: separate filename characters and numbers

From: Branko

Date: 18 Mar, 2010 08:09:04

Message: 2 of 11

"Rajesh " <rv_acharya@rediffmail.com> wrote in message <hnsj1k$hcv$1@fred.mathworks.com>...
> Hi,
>
> I have a directory with multiple tif images named as say 'file1','file2' ...file100 and so on.
> To begin with i will not be knowing what the exact name is like e.g. the names may also be like image1, image2.... or file_001, file_002 etc.
>
> Is there a way to parse the file name by say picking a file from the directory and breaking the name into characters and numbers and then use a for loop to open the files one after another?
>
> There may be other files in the directory other than the series of files that i am interested in and i dont want to read / open them.
>
> I saw strtok,strcmp,strfind,regexp but as i understood it requires a character or a set of characters to compare the entire string.
>
> thanks..

doc dir

Branko

Subject: separate filename characters and numbers

From: Rune Allnor

Date: 18 Mar, 2010 08:52:19

Message: 3 of 11

On 18 Mar, 08:00, "Rajesh " <rv_acha...@rediffmail.com> wrote:
> Hi,
>
> I have a directory with multiple tif images named as say 'file1','file2' ...file100 and so on.
> To begin with i will not be knowing what the exact name is like e.g. the names may also be like image1, image2.... or file_001, file_002 etc.
>
> Is there a way to parse the file name by say picking a file from the directory and breaking the name into characters and numbers and then use a for loop to open the files one after another?
>
> There may be other files in the directory other than the series of files that i am interested in and i dont want to read / open them.
>
> I saw strtok,strcmp,strfind,regexp but as i understood it requires a character or a set of characters to compare the entire string.

You will need to know quite a lot about the names of the files
you want to process. It is not sufficient to know that there
are a set of sequentially numbered file names.

Use the DIR command to get a list of files in the directory.
Then use the FILEPARTS command to split each file name into
path, filename and file extension. If the file name extension
matches the files you look for, use a regular expression to
first of all check that the file name is on the prescribed format,
and also to extract the sequenctial number from the file name.

This might seem like a lot of work, but it becomes second nature
once you have done it a couple of times. The hard part is to
learn how to work with regular expressions. I know no good
online tutorials, but would recommend the book

http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=sr_1_fkmr0_1?ie=UTF8&qid=1268902227&sr=8-1-fkmr0

Rune

Subject: separate filename characters and numbers

From: Rajesh

Date: 18 Mar, 2010 12:17:40

Message: 4 of 11

Thanks Rune and Bracko

But my question still remains. Once i get the filename I need to break it in two parts 1. String and 2. Number and then use the string and any number within the range to open a certain file or a sequence of files using a for loop.

Is there a function which can scan the string, character by character, and stop when it finds a number so that the text before that index number becomes my string (part1) and the rest the number (part2)

Subject: separate filename characters and numbers

From: Rune Allnor

Date: 18 Mar, 2010 12:22:47

Message: 5 of 11

On 18 Mar, 13:17, "Rajesh " <rv_acha...@rediffmail.com> wrote:
> Thanks Rune and Bracko
>
> But my question still remains. Once i get the filename I need to break it in two parts 1. String and 2. Number and then use the string and any number within the range to open a certain file or a sequence of files using a for loop.

No, you don't.

You only need to know that the file belongs to the set
you are looking for. The order in which you process
the files doesn't matter - or you have not said it does.

And even if the sequence *should* matter, I have already
told you how to do this: Use regular expressions. That's
what they are for.

Rune

Subject: separate filename characters and numbers

From: Branko

Date: 18 Mar, 2010 12:59:05

Message: 6 of 11

"Rajesh " <rv_acharya@rediffmail.com> wrote in message <hnt5l4$4k8$1@fred.mathworks.com>...
> Thanks Rune and Bracko
>
> But my question still remains. Once i get the filename I need to break it in two parts 1. String and 2. Number and then use the string and any number within the range to open a certain file or a sequence of files using a for loop.
>
> Is there a function which can scan the string, character by character, and stop when it finds a number so that the text before that index number becomes my string (part1) and the rest the number (part2)

One of solutions:

fnam=dir('*.tif'); % list of all tif files

% and then use for loop to open files
for ii = 1:length(fnam)
    img{ii}=imread(fnam(ii).name);
end

If you would like to split filename (char&num) look regexp.

Branko

Subject: separate filename characters and numbers

From: Loren Shure

Date: 19 Mar, 2010 12:05:29

Message: 7 of 11

In article <hnsj1k$hcv$1@fred.mathworks.com>, rv_acharya@rediffmail.com
says...
> Hi,
>
> I have a directory with multiple tif images named as say 'file1','file2' ...file100 and so on.
> To begin with i will not be knowing what the exact name is like e.g. the names may also be like image1, image2.... or file_001, file_002 etc.
>
> Is there a way to parse the file name by say picking a file from the directory and breaking the name into characters and numbers and then use a for loop to open the files one after another?
>
> There may be other files in the directory other than the series of files that i am interested in and i dont want to read / open them.
>
> I saw strtok,strcmp,strfind,regexp but as i understood it requires a character or a set of characters to compare the entire string.
>
> thanks..
>

The function isletter may be of some help.

--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: separate filename characters and numbers

From: sk maidul haque

Date: 11 May, 2011 14:17:20

Message: 8 of 11

sprintf or sscanf can probably help you to do that.


"Rajesh" wrote in message <hnsj1k$hcv$1@fred.mathworks.com>...
> Hi,
>
> I have a directory with multiple tif images named as say 'file1','file2' ...file100 and so on.
> To begin with i will not be knowing what the exact name is like e.g. the names may also be like image1, image2.... or file_001, file_002 etc.
>
> Is there a way to parse the file name by say picking a file from the directory and breaking the name into characters and numbers and then use a for loop to open the files one after another?
>
> There may be other files in the directory other than the series of files that i am interested in and i dont want to read / open them.
>
> I saw strtok,strcmp,strfind,regexp but as i understood it requires a character or a set of characters to compare the entire string.
>
> thanks..

Subject: separate filename characters and numbers

From: Kelly Kearney

Date: 11 May, 2011 15:19:04

Message: 9 of 11

"Rajesh" wrote in message <hnt5l4$4k8$1@fred.mathworks.com>...
> Thanks Rune and Bracko
>
> But my question still remains. Once i get the filename I need to break it in two parts 1. String and 2. Number and then use the string and any number within the range to open a certain file or a sequence of files using a for loop.
>
> Is there a function which can scan the string, character by character, and stop when it finds a number so that the text before that index number becomes my string (part1) and the rest the number (part2)


As Rune suggested, regular expressions are your best bet. If you just want to find all files that match the text-number.tif format, this regular expression will do the trick. I recommend you take the time to read the Regular Expressions section in Matlab's documentation, and figure out what the pattern I've provided below means, since this sort of string parsing can be a very useful tool.

%-----

% Find files matching pattern

a = dir('*.nc');
matching = regexp({a.name}, '^[a-zA-Z_]+[0-9]+\.tif$', 'match', 'once');
matching = matching(~cellfun('isempty', matching));

% Group files by common base (in case you have multiple series in one folder)

str = unique(regexp(matching, '^[a-zA-Z_]*', 'match', 'once'));
str = str(~cellfun('isempty', str));
group = cell(size(str));
for is = 1:length(str)
    ismatch = strncmp(str{is}, matching, length(str{is}));
    group{is} = matching(ismatch);
end

%-----

If you need to process the files in order, things might get a little more tricky, since the alphabetical order returned above may not be the same as numerical order, and you'd have to know the format of the number (1-10 vs 01-10, for example) in order to recreate the filenames once you've extracted the numbers.

Subject: separate filename characters and numbers

From: Andy

Date: 11 May, 2011 15:39:04

Message: 10 of 11

Rajesh, could you tell us more about why you think you need to split the filenames into characters and numbers? The reason most users are just suggesting dir is because splitting the filenames this way is very likely the wrong way to accomplish your goal, but you haven't told us what your actual goal is.

Subject: separate filename characters and numbers

From: Rajesh

Date: 23 May, 2011 04:56:04

Message: 11 of 11

"Andy" wrote in message <iqeaio$9uf$1@newscl01ah.mathworks.com>...
> Rajesh, could you tell us more about why you think you need to split the filenames into characters and numbers? The reason most users are just suggesting dir is because splitting the filenames this way is very likely the wrong way to accomplish your goal, but you haven't told us what your actual goal is.

Thanks one and all... ( Rune, Brancko, Loren, Sk maidul, Kely and Andy)....
I'll reply to Andys question above. I am gettng a set of tif images named image1, image2... image100 etc. from the user. The kink here is that i do not know what the image names might be (obviously) but they will be in some variations of the above form for sure. Now, in my code i am accessing the image1.tif file first, doing my mathematics and moving on to the next file in the sequence viz. image2 . right now i have only got the directory and there is no way of knowing which is the first file to be opened and how to go about this business in sequence.
I have tried the sorting option on 'filename.name' which leads to the following sequence image1, image10, image100, image2, image20, image 3 etc... and not image 1 2 3 .... thats why i decided to parse the filename and look for the numbers.
may be a simpler option exists, may be i am doing something wrong in sorting or missing something there,
hope i am clear.
thanks for the response anyway, i am working on regular expressions.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us