Hi,
today a problem occurred, I thought it's kind of a no-brainer, but I was wrong:
If I get the pathname of my current simulink block, I get the following
>> gcb
ans = Mechanics/Spring and Damping Elements/mech_sprdam_2mass
But I can't separate the path. If I ask for my file separator, I get
>> filesep
ans = \
Why is my file separator windows-like, but my given path is in unix style? Is there a way to write my script working on both systems?

댓글 수: 3

Stephen23
Stephen23 2016년 2월 4일
What operation do you want to perform on the string?
geieraffe
geieraffe 2016년 2월 4일
I need the separated parts of the string to find them in a documentation.
Stephen23
Stephen23 2016년 2월 4일
Then use the regexp-based code in my answer.

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

 채택된 답변

Subhadra Mahanti
Subhadra Mahanti 2016년 2월 4일

1 개 추천

When you are writing MATLAB scripts to get or set block names or paths, use forward slashes as forward-slashes are always used as the separators for block names/paths regardless of the platform.

추가 답변 (1개)

Stephen23
Stephen23 2016년 2월 4일
편집: Stephen23 2016년 2월 4일

2 개 추천

This is a very broad question, because it depends entirely on what operations you wish to perform on that string: do you wish to split the paths, create longer filepaths, find root directories, save data, use as a function input (filereading, dir, etc) ... ? Each of these could have quite a different solution, but we can't really give advice until you tell us what you are doing with that path string.
Have a look at my FEX submission natsortfiles, where I use regexp to split up a path string:
%pth = regexp(pth,filesep,'split'); % OS dependent
pth = regexp(pth,'[/\\]','split'); % either / or \
I tried the first line, and then realized that the second avoids problems like that one you have, so I changed to the second line.

카테고리

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

질문:

2016년 2월 4일

댓글:

2016년 2월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by