Extract part of filepath and using like legendname

17 views (last 30 days)
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!

Accepted Answer

Adam Danz
Adam Danz on 24 Jun 2020
Edited: Adam Danz on 25 Jun 2020
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.

More Answers (0)

Categories

Find more on Search Path in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!