Separate path string into drive and folders

298 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 24 Lug 2013
s='C:\user\files\myFile.txt'
out=regexp(s,'\','split')
  2 Commenti
Manuel Lera Ramirez
Manuel Lera Ramirez il 5 Giu 2018
Modificato: Manuel Lera Ramirez il 5 Giu 2018
For a general solution (would work on any OS) I would suggest
endout=regexp(s,filesep,'split')
Best
baby
baby il 25 Feb 2022
works for me as well.

Accedi per commentare.

Più risposte (2)

Christian Schwermer
Christian Schwermer il 9 Set 2018
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 il 19 Lug 2023
path = uigetdir()
foldername = strsplit(path,"\");
foldername = foldername(end);

Categorie

Scopri di più su File Name Construction in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by