Thread Subject: Parsing a filename

Subject: Parsing a filename

From: Rachel Laughs

Date: 17 Nov, 2009 16:39:21

Message: 1 of 6

user selects a file using "uigetfile" and now I want to extract text from that file name. The file names will always be in a variation of this format:

directory\The_File_01.ext
directory\TheFile_01.ext

(where the extension is always the same, and there is ALWAYS two number digits following an underscore before the extension)

What I need to collect:
1. the pathname
2. the filename WITHOUT the last two numbers, but INCLUDING all of the underscores
3. the two numbers following the final underscore

any ideas? I've run fresh out. thanks!

Subject: Parsing a filename

From: Miroslav Balda

Date: 17 Nov, 2009 17:10:25

Message: 2 of 6

"Rachel Laughs" <remove.thisrachellaughs@gmail.com> wrote in message <hdujjo$224$1@fred.mathworks.com>...
> user selects a file using "uigetfile" and now I want to extract text from that file name. The file names will always be in a variation of this format:
>
> directory\The_File_01.ext
> directory\TheFile_01.ext
>
> (where the extension is always the same, and there is ALWAYS two number digits following an underscore before the extension)
>
> What I need to collect:
> 1. the pathname
> 2. the filename WITHOUT the last two numbers, but INCLUDING all of the underscores
> 3. the two numbers following the final underscore
>
> any ideas? I've run fresh out. thanks!

Hi,
the following code solves your task:
     [filename,pth] = uigetfile(......); % The path is in pth
     I = findstr('_',filename);
     file = filename(1:I(end));
Mira

Subject: Parsing a filename

From: Rachel Laughs

Date: 17 Nov, 2009 18:02:19

Message: 3 of 6

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <hdule1$rkp$1@fred.mathworks.com>...
> Hi,
> the following code solves your task:
> [filename,pth] = uigetfile(......); % The path is in pth
> I = findstr('_',filename);
> file = filename(1:I(end));
> Mira

ah, that is brilliant!! Thank you very much. I was not aware of the findstr function and your genius use of it! thanks!!

by the way, what would you suggest for extracting the two numbers at the end of the filename? Something about textscan perhaps?

Subject: Parsing a filename

From: Rachel Laughs

Date: 17 Nov, 2009 18:09:18

Message: 4 of 6

"Rachel Laughs" <remove.thisrachellaughs@gmail.com> wrote in message <hduofa$8u1$1@fred.mathworks.com>...
>
> by the way, what would you suggest for extracting the two numbers at the end of the filename? Something about textscan perhaps?


oh wait, nevermind. I just expanded on your use of "I"

thanks again

Subject: Parsing a filename

From: Loren Shure

Date: 17 Nov, 2009 20:34:54

Message: 5 of 6

In article <hdujjo$224$1@fred.mathworks.com>,
remove.thisrachellaughs@gmail.com says...
> user selects a file using "uigetfile" and now I want to extract text from that file name. The file names will always be in a variation of this format:
>
> directory\The_File_01.ext
> directory\TheFile_01.ext
>
> (where the extension is always the same, and there is ALWAYS two number digits following an underscore before the extension)
>
> What I need to collect:
> 1. the pathname
> 2. the filename WITHOUT the last two numbers, but INCLUDING all of the underscores
> 3. the two numbers following the final underscore
>
> any ideas? I've run fresh out. thanks!
>

You might check out the function fileparts

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

Subject: Parsing a filename

From: Miroslav Balda

Date: 17 Nov, 2009 21:25:19

Message: 6 of 6

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <hdule1$rkp$1@fred.mathworks.com>...
> "Rachel Laughs" <remove.thisrachellaughs@gmail.com> wrote in message <hdujjo$224$1@fred.mathworks.com>...
:
SNIP
:
> > 3. the two numbers following the final underscore
:
> Hi,
> the following code solves your task:
> [filename,pth] = uigetfile(......); % The path is in pth
> I = findstr('_',filename);
> file = filename(1:I(end));

Sorry, I have missed to answer the third question:
numb = filename(I+1:I+2);
Mira

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
parsing Rachel Laughs 17 Nov, 2009 11:44:05
uigetfile Rachel Laughs 17 Nov, 2009 11:44:04
rssFeed for this Thread

Contact us at files@mathworks.com