Extract part of filepath and using like legendname

조회 수: 16 (최근 30일)
Nik Rocky
Nik Rocky 2020년 6월 24일
댓글: Nik Rocky 2020년 6월 25일
Hello,
I have paths like this:
FilePath = '/home/user/workspace/QT/Software_2.0_QT/IO/Motor_Set_1/AKG_C1000S/Distance_0.5m/Scenario_M1/001_m1_60_const_20200611_200515/SNR_F1/'
and I need to save (in a loop)
Save_path{k} = 'Motor_Set_1/AKG_C1000S/Distance_0.5m/Scenario_M1/001_m1_60_const_20200611_200515/'
or maybe I can set it also shorter (for a plot legend)!?
Save_path{k} = 'MS1/AKG/0.5m/M1/001/'
?
Variables are:
Motor_Set_1, Motor_Set_2, Motor_Set_3
AKG_C1000S, SONY_D50
Distance_0.5m, Distance_2m
Scenario_M1, Scenario_M2, Scenario_M4
001_m1_..., 002_m1_m2..., ..., 030_m4_m3...
How can I cut it?
Thank you!

채택된 답변

Adam Danz
Adam Danz 2020년 6월 24일
편집: Adam Danz 2020년 6월 25일
Make sure all slashes are forward shashes
FilePath = strrep(FilePath, '\','/');
Replace any double shashes with single slashes
strrep(FilePath, '//','/')
Find the position of the slashes
slashIdx = strfind(FilePath, '/');
Choose which segments of the path you want to extract (here I chose segment 10 to 13)
pathSegment = FilePath(slashIdx(10)+1:slashIdx(13)-1)
The +1 and -1 remove the slash from the beginning and end of the segment.
You could also use regular expressions or some of the other string search functions if you want to search for a specific starting and ending directory.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by