Remove text after a symbol

91 visualizzazioni (ultimi 30 giorni)
alejandro paradiso
alejandro paradiso il 16 Nov 2022
Commentato: Les Beckham il 16 Nov 2022
Good day to everyone, i'm stuck with separate/erase some words from a sentence.
I'm filtering some data from a table and I don't want to keep anything after the space before hyphen.
Column is already a string
'Bohemian Rhapsody - Remastered 2011'
'(I Can''t Get No) Satisfaction - Mono Version'
'My Generation - Stereo Version'
'Hotel California - 2013 Remaster'
'Born to Run'
'Tears in Heaven - Acoustic; Live at MTV Unplugged, Bray Film Studios, Windsor, England, UK, 1/16/1992; 2013 Remaster'
I've tried with extractAfter but if the name does not contain hyphen, like Born to Run, the outcome is zero or missing. I've also created a pattern with wildcardPattern but, as you can the variations are too much
thanks everyone who will answer

Risposta accettata

Les Beckham
Les Beckham il 16 Nov 2022
If the titles are character vectors in a cell array:
titles = ...
{ 'Bohemian Rhapsody - Remastered 2011'
'(I Can''t Get No) Satisfaction - Mono Version'
'My Generation - Stereo Version'
'Hotel California - 2013 Remaster'
'Born to Run'
'Tears in Heaven - Acoustic; Live at MTV Unplugged, Bray Film Studios, Windsor, England, UK, 1/16/1992; 2013 Remaster' };
short_titles = titles; % copy the original titles
idx = contains(short_titles, ' -'); % find the ones that contain ' -'
short_titles(idx) = extractBefore(short_titles(idx), ' -') % remove the unwanted parts from those
short_titles = 6×1 cell array
{'Bohemian Rhapsody' } {'(I Can't Get No) Satisfaction'} {'My Generation' } {'Hotel California' } {'Born to Run' } {'Tears in Heaven' }
The same code also works if they are in a string array:
titles = ...
[ "Bohemian Rhapsody - Remastered 2011"
"(I Can't Get No) Satisfaction - Mono Version"
"My Generation - Stereo Version"
"Hotel California - 2013 Remaster"
"Born to Run"
"Tears in Heaven - Acoustic; Live at MTV Unplugged, Bray Film Studios, Windsor, England, UK, 1/16/1992; 2013 Remaster" ];
short_titles = titles; % copy the original titles
idx = contains(short_titles, ' -'); % find the ones that contain ' -'
short_titles(idx) = extractBefore(short_titles(idx), ' -') % remove the unwanted parts from those
short_titles = 6×1 string array
"Bohemian Rhapsody" "(I Can't Get No) Satisfaction" "My Generation" "Hotel California" "Born to Run" "Tears in Heaven"
  2 Commenti
alejandro paradiso
alejandro paradiso il 16 Nov 2022
thank you Les, one additional question. I've tried with extractBefore, but the outcome was zero if the song name didn't contain hyphen.
why with indexed extractBefore(short_titles(idx), ' -') is working as I thought?
Les Beckham
Les Beckham il 16 Nov 2022
You are quite welcome.
The result will be empty (0x0 char), not actually zero, if you try to use extractBefore and the string doesn't contain what you are looking for. That is why we need the extra step of finding the ones that definitely contain the search pattern and then only using extractBefore on them.
titles = ...
{ 'Bohemian Rhapsody - Remastered 2011'
'(I Can''t Get No) Satisfaction - Mono Version'
'My Generation - Stereo Version'
'Hotel California - 2013 Remaster'
'Born to Run'
'Tears in Heaven - Acoustic; Live at MTV Unplugged, Bray Film Studios, Windsor, England, UK, 1/16/1992; 2013 Remaster' };
extractBefore(titles, ' -')
ans = 6×1 cell array
{'Bohemian Rhapsody' } {'(I Can't Get No) Satisfaction'} {'My Generation' } {'Hotel California' } {0×0 char } {'Tears in Heaven' }

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB Report Generator in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by