Separate path string into drive and folders

Hi,
using fileparts brings the path, the name and the exentension of a fullfile (e.g., C:\user\files\myFile.txt). Is there a function that separates the path into drive and folders, so that drive = 'C:' folder1 = 'user' folder2 = 'files' ?
Thx, Philipp

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 24 日

8 投票

s='C:\user\files\myFile.txt'
out=regexp(s,'\','split')

6 件のコメント

Manuel Lera Ramirez
Manuel Lera Ramirez 2018 年 6 月 5 日
編集済み: Manuel Lera Ramirez 2018 年 6 月 5 日
For a general solution (would work on any OS) I would suggest
endout=regexp(s,filesep,'split')
Best
baby
baby 2022 年 2 月 25 日
works for me as well.
Riad
Riad 2024 年 5 月 17 日
Hello,
filesep is equivalent to "\" and it works fine for a kind of paths. But I've troubles when I want to separate paths that use this separator "/". I mean: when you derive blocks paths (using "find_system") you'll get paths like this : 'Swc_MTR/Run_MTR_1ms_sys/COMP_/In1' and :
regexp('Swc_MTR/Run_MTR_1ms_sys/COMP_/In1',filesep,'split')
won't work !
I want to avoid strsplit function by using a function that can work for both Windows/Linux OS !
Anyone can help ?
Stephen23
Stephen23 2024 年 5 月 17 日
編集済み: Stephen23 2024 年 5 月 17 日
@Mohamed Riad: because you are already using REGEXP this is quite easy:
regexp('Swc_MTR/Run_MTR_1ms_sys/COMP_/In1','[/\\]+','split')
ans = 1x4 cell array
{'Swc_MTR'} {'Run_MTR_1ms_sys'} {'COMP_'} {'In1'}
Riad
Riad 2024 年 5 月 21 日
Hi @Stephen23 there more easier way by using strsplit but I want to avoid using either "/" or "\" in the script (on the server, I've a Linux OS):
strsplit('Swc_MTR/Run_MTR_1ms_sys/COMP_/In1','/')
ans =
1×4 cell array
{'Swc_MTR'} {'Run_MTR_1ms_sys'} {'COMP_'} {'In1'}
Stephen23
Stephen23 2024 年 5 月 21 日
"filesep is equivalent to "\" and it works fine for a kind of paths"
No, what FILESEP is depends on the OS that MATLAB is currently running on.
"I want to avoid strsplit function by using a function that can work for both Windows/Linux OS !"
And now you write that you want to use STRSPLIT: your requirements keep on changing.
"I want to avoid using either "/" or "\" in the script (on the server, I've a Linux OS)"
I thought the requirement is to split on either of the path separators, which is also easy with STRSPLIT:
strsplit('Swc_MTR/Run_MTR_1ms_sys/COMP_/In1',{'/','\'})
ans = 1x4 cell array
{'Swc_MTR'} {'Run_MTR_1ms_sys'} {'COMP_'} {'In1'}

サインインしてコメントする。

その他の回答 (2 件)

Christian Schwermer
Christian Schwermer 2018 年 9 月 9 日

6 投票

The shortest solution is to split the string using filesep as delimeter. filesep returns the platform-specific file separator "\" or "/".
pathparts = strsplit(s,filesep);
Dominique
Dominique 2023 年 7 月 19 日

0 投票

path = uigetdir()
foldername = strsplit(path,"\");
foldername = foldername(end);

カテゴリ

ヘルプ センター および File ExchangeFilename Construction についてさらに検索

質問済み:

2013 年 7 月 24 日

コメント済み:

2024 年 5 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by