Condense code in Split string operation

1 view (last 30 days)
I have a excel file with the name 'xxx_yyy_zzz.xlsx'
Here, I want to extract only the part 'zzz' and for that I am using the following code
name = 'xxx_yyy_zzz.xlsx';
dummy1 = split('xxx_yyy_zzz.xlsx', '.');
dummy2 = split(dummy1{1}, '_');
final = dummy2{end};
My question is -- is it posible to condense these four lines into a single line of code?

Accepted Answer

Stephen23 on 13 Sep 2022
s = "xxx_yyy_zzz.xlsx";
ans = "zzz"
  1 Comment
Sanju Das
Sanju Das on 14 Sep 2022
Edited: Sanju Das on 14 Sep 2022
It works, thanks for the help

Sign in to comment.

More Answers (1)

Les Beckham
Les Beckham on 13 Sep 2022
If you put the filename in a string instead of a char vector you can do it in one line
s = "xxx_yyy_zzz.xlsx"
s = "xxx_yyy_zzz.xlsx"
extractBetween(s, '_', '_')
ans = "yyy"
If it is a char vector it will take two lines
s = 'xxx_yyy_zzz.xlsx'
s = 'xxx_yyy_zzz.xlsx'
s1 = extractBetween(s, '_', '_')
s1 = 1×1 cell array
s2 = s1{:}
s2 = 'yyy'


Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by