Why does strtok cut too much out of my string?

1 view (last 30 days)
Hi,
I am using the strtok function to cut out a certain part of my strings. I have a lot of filenames with the exact string in the beginning of the name and would like to get out the last part of the string.
Eg: Filenmes{1,1} = 'ID_setup_CMJ_L_2' Filenmes{2,1} = 'ID_setup_DJ_R_1'
So I want to get something like:
Names{1,1} = 'CMJ_L_2' Names{2,1} = 'DJ_R_1'
I use the following line for this:
[prt1, prt2] = strtok(filenmes{i,1},'ID_setup_')
It works fine for all of my filenames except for the DJ's.. then it gives me Names({2,1} = 'J_R_1' (instead of DJ_R_1).
Thus, it cuts out that 'D' which it does not do in all of my other examples (CMJ,NonCMJ,HH,...)
Any suggestions? Are there perhaps better ways to do this?
Thanks!

Accepted Answer

Thorsten
Thorsten on 28 Oct 2015
Edited: Thorsten on 28 Oct 2015
This is because you specify a set of delimiters in the second argument of the strtok function, and 'D' is a delimiter. Leading delimiters are ignored, so the D in JD is cut.
Instead of strtok, you can may want to use strrep
newfilename = strrep(filenmes{i,1},'ID_setup_', '');

More Answers (0)

Categories

Find more on Characters and Strings 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!