Separate path string into drive and folders
224 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Azzi Abdelmalek
2013-7-24
s='C:\user\files\myFile.txt'
out=regexp(s,'\','split')
6 个评论
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
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',{'/','\'})
更多回答(2 个)
Christian Schwermer
2018-9-9
The shortest solution is to split the string using filesep as delimeter. filesep returns the platform-specific file separator "\" or "/".
pathparts = strsplit(s,filesep);
0 个评论
Dominique
2023-7-19
path = uigetdir()
foldername = strsplit(path,"\");
foldername = foldername(end);
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!