hello, how could I take part from string for ex. I have this path : C:\Users\khs\Desktop\pics\photos and want matlab to make it C:\Users\khs\Desktop\pics\ futhermore want it to take the name of folder ( khs ) please help me thanks

 채택된 답변

Jan
Jan 2018년 3월 20일
편집: Jan 2018년 3월 20일

1 개 추천

S = 'C:\Users\khs\Desktop\pics\photos';
[PathName, Folder] = fileparts(S);
Or more flexible:
S = 'C:\Users\khs\Desktop\pics\photos';
C = strsplit(S, '\');
PathName = sprintf('%s\\', C{1:end-1});
PathName(end) = [];
Folder = C{end};

댓글 수: 3

Khaldon Soued
Khaldon Soued 2018년 3월 20일
perfekt danke sehr .. thank you so much :)
one more question please how to change cell array to cells
Jan
Jan 2018년 3월 20일
The question is not clear: "cells" is just a different name for "cell array". So what do you want to "change" exactly?
Be cautious in generalizing the "more flexible" code for use with Linux or Mac. The obvious approach is to use something like
C = strsplit(regexprep(S, '\\', '/'), '/')
which changes all \ characters to /: this produces valid MS Windows paths because MS Windows permits both \ and / as the path separator. The problem encountered is that \ is a valid character in Linux and Mac filenames, so a file name such as 'hi\tlo' is a valid 6-character file name (just not a great one, as people would tend to think the \t was a tab character.) The replaced version would be 'hi/tlo' which would not refer to the same place at all even if it existed.
Thus, the generalized version would have to only translate \ to / if ispc() is true

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Filename Construction에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2018년 3월 20일

댓글:

2018년 8월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by