How to get the path part of a filename?

I want to get the path part of a file name in MATLAB like dirname and basename in Linux. I've tried to turn to find a function like strrchr, but I failed. I know strtok, strfind and textscan can be used, but I want to accomplish this with not more than two statements.


For this particular problem I suggest you use the fileparts function:

[path, filename, extention] = fileparts(str)

Nick's answer definitely does what you ask for, but here's an alternative answer using regexprep:

regexprep(str, '(.+)(?:\\|/)(.*)', '$1')

If you'd want to capture the filename (extension included), use the $2 token instead of $1. It's a good exercise for regular expressions, which prove to be very useful in MATLAB when parsing text.

Need Your Help

How to update the screenshot of an app on IOS 7


I was reading a very interesting article about the Pinterest app updated for IOS 7. One of the new things added was the possibility to see in the screenshot of the app if there are any new updates:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.