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:
How to seperate the directory and filename from a string ?

Subject: How to seperate the directory and filename from a string ?

From: Jane

Date: 15 Sep, 2010 14:13:20

Message: 1 of 9

Hello,

I have trouble to get filename and directory from a string.
Here's my string looks like:

s = 'C:\product1\opt\serialnumber1\wpc_product1_serialnumber1.opt'

The number of the subdirectory layers varies.

Many thanks,
Jane

Subject: How to seperate the directory and filename from a string ?

From: Sean

Date: 15 Sep, 2010 14:24:18

Message: 2 of 9

"Jane " <jane.wang@seagate.com> wrote in message <i6qka0$fug$1@fred.mathworks.com>...
> Hello,
>
> I have trouble to get filename and directory from a string.
> Here's my string looks like:
>
> s = 'C:\product1\opt\serialnumber1\wpc_product1_serialnumber1.opt'
>
> The number of the subdirectory layers varies.
>
> Many thanks,
> Jane

Find the last '\':
Everything before it and it are file path everything else is file name:

idx = max(strfind(s,'\');
my_path = s(1:idx);
my_file_name = s(idx+1:end);

%Sean

Subject: How to seperate the directory and filename from a string ?

From: Oleg Komarov

Date: 15 Sep, 2010 14:25:22

Message: 3 of 9

"Jane " <jane.wang@seagate.com> wrote in message <i6qka0$fug$1@fred.mathworks.com>...
> Hello,
>
> I have trouble to get filename and directory from a string.
> Here's my string looks like:
>
> s = 'C:\product1\opt\serialnumber1\wpc_product1_serialnumber1.opt'
>
> The number of the subdirectory layers varies.
>
> Many thanks,
> Jane

lbslash = find(s == '\',1,'last');
name = s(lbslash+1:end);
dire = s(1:lbslash);

Oleg

Subject: How to seperate the directory and filename from a string ?

From: Steven_Lord

Date: 15 Sep, 2010 14:27:48

Message: 4 of 9



"Jane " <jane.wang@seagate.com> wrote in message
news:i6qka0$fug$1@fred.mathworks.com...
> Hello,
>
> I have trouble to get filename and directory from a string. Here's my
> string looks like:
> s = 'C:\product1\opt\serialnumber1\wpc_product1_serialnumber1.opt'
>
> The number of the subdirectory layers varies.

Look at FILEPARTS.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to seperate the directory and filename from a string ?

From: ImageAnalyst

Date: 15 Sep, 2010 14:36:08

Message: 5 of 9

Jane:
I'd also like to put in a plug for John's "allwords"
http://www.mathworks.com/matlabcentral/fileexchange/27184-allwords

This is also a very nice program for splitting up a directory path.
For example:
 folder = 'C:\Program Files\MATLAB\work\UserExamples'
words = allwords(folder, '\')
words =
    'C:' 'Program Files' 'MATLAB' 'work' 'UserExamples'
So, for example, you could use this to get the name of a parent
directory, or go up 2 directory levels, or go to the 3rd directory
level, or even to build up the name of a file (such as an Excel file)
based on the folder name (such as "Results for UserExamples.xls). So I
think it can be used in quite flexible ways.

Subject: How to seperate the directory and filename from a string ?

From: Jane

Date: 15 Sep, 2010 18:36:10

Message: 6 of 9

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6qkui$soc$1@fred.mathworks.com>...
> "Jane " <jane.wang@seagate.com> wrote in message <i6qka0$fug$1@fred.mathworks.com>...
> > Hello,
> >
> > I have trouble to get filename and directory from a string.
> > Here's my string looks like:
> >
> > s = 'C:\product1\opt\serialnumber1\wpc_product1_serialnumber1.opt'
> >
> > The number of the subdirectory layers varies.
> >
> > Many thanks,
> > Jane
>
> Find the last '\':
> Everything before it and it are file path everything else is file name:
>
> idx = max(strfind(s,'\');
> my_path = s(1:idx);
> my_file_name = s(idx+1:end);
>
> %Sean

Thank you, Sean. All I tried was to define a format to sscanf the filename. This is a very good solution.

Jane

Subject: How to seperate the directory and filename from a string ?

From: Jane

Date: 15 Sep, 2010 18:41:05

Message: 7 of 9

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <i6ql0h$3k3$1@fred.mathworks.com>...
> "Jane " <jane.wang@seagate.com> wrote in message <i6qka0$fug$1@fred.mathworks.com>...
> > Hello,
> >
> > I have trouble to get filename and directory from a string.
> > Here's my string looks like:
> >
> > s = 'C:\product1\opt\serialnumber1\wpc_product1_serialnumber1.opt'
> >
> > The number of the subdirectory layers varies.
> >
> > Many thanks,
> > Jane
>
> lbslash = find(s == '\',1,'last');
> name = s(lbslash+1:end);
> dire = s(1:lbslash);
>
> Oleg
I got it. Thank you, Oleg.

Subject: How to seperate the directory and filename from a string ?

From: someone

Date: 15 Sep, 2010 18:48:04

Message: 8 of 9

"Jane " <jane.wang@seagate.com> wrote in message <i6r3mp$g8p$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6qkui$soc$1@fred.mathworks.com>...
> > "Jane " <jane.wang@seagate.com> wrote in message <i6qka0$fug$1@fred.mathworks.com>...
> > > Hello,
> > >
> > > I have trouble to get filename and directory from a string.
> > > Here's my string looks like:
> > >
> > > s = 'C:\product1\opt\serialnumber1\wpc_product1_serialnumber1.opt'
> > >
> > > The number of the subdirectory layers varies.
> > >
> > > Many thanks,
> > > Jane
> >
> > Find the last '\':
> > Everything before it and it are file path everything else is file name:
> >
> > idx = max(strfind(s,'\');
> > my_path = s(1:idx);
> > my_file_name = s(idx+1:end);
> >
> > %Sean
>
> Thank you, Sean. All I tried was to define a format to sscanf the filename. This is a very good solution.

% But it is platform dependent (Windows only).
% As Steven Lord said,

doc fileparts

% is the way to go.
% And it will work on windows, unix, etc.
>
> Jane

Subject: How to seperate the directory and filename from a string ?

From: Jane

Date: 15 Sep, 2010 19:00:21

Message: 9 of 9

"someone" <someone@somewhere.net> wrote in message <i6r4d4$2uf$1@fred.mathworks.com>...
> "Jane " <jane.wang@seagate.com> wrote in message <i6r3mp$g8p$1@fred.mathworks.com>...
> > "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6qkui$soc$1@fred.mathworks.com>...
> > > "Jane " <jane.wang@seagate.com> wrote in message <i6qka0$fug$1@fred.mathworks.com>...
> > > > Hello,
> > > >
> > > > I have trouble to get filename and directory from a string.
> > > > Here's my string looks like:
> > > >
> > > > s = 'C:\product1\opt\serialnumber1\wpc_product1_serialnumber1.opt'
> > > >
> > > > The number of the subdirectory layers varies.
> > > >
> > > > Many thanks,
> > > > Jane
> > >
> > > Find the last '\':
> > > Everything before it and it are file path everything else is file name:
> > >
> > > idx = max(strfind(s,'\');
> > > my_path = s(1:idx);
> > > my_file_name = s(idx+1:end);
> > >
> > > %Sean
> >
> > Thank you, Sean. All I tried was to define a format to sscanf the filename. This is a very good solution.
>
> % But it is platform dependent (Windows only).
> % As Steven Lord said,
>
> doc fileparts
>
> % is the way to go.
> % And it will work on windows, unix, etc.
> >
> > Jane

Hi, Steve, ImageAnalyst and Someone,

Thank you very much. I don't know there's a "fileparts". It is the command to meet my purpose. The "allwords" could be easily modified to parse my other files. I appreciate your help.

Jane

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