Extracting specific words from cells within a table column and moving them to new column
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
greenyellow22
el 15 de Jul. de 2022
Editada: Campion Loong
el 20 de Jul. de 2022
Hello! :)
I have a table with multiple columns, whereby one of the columns includes text.
The column looks somewhat like that. Please note that there are way more rows, and I want to address all of them with the code and not just the ones presented here.
'New Scene was loaded: CastleDown'
'Game is trying to save file'
'Saving successful.'
'New Game File created successfully.'
'New Scene was loaded: Outside'
'Periodic Save...'
'Coin collected'
...
What I would like to do is to split only those rows who contain the text "New Scene was loaded: XY", whereby I want to extract the last word, such as "CastleDown" or "Outside" and move it to a new column.
So it should look somewhat like that:
'New Scene was loaded:' 'CastleDown'
'Game is trying to save file' ''
'Saving successful.' ''
'New Game File created successfully.' ''
'New Scene was loaded: Outside' 'Outside'
'Periodic Save...' ''
'Coin collected' ''
... ...
Then, I would also like to enter the text extracted and move to the other column to also fill the empty rows below (until the next word), looking like that:
'New Scene was loaded:' 'CastleDown'
'Game is trying to save file' 'CastleDown'
'Saving successful.' 'CastleDown'
'New Game File created successfully.' 'CastleDown'
'New Scene was loaded: Outside' 'Outside'
'Periodic Save...' 'Outside'
'Coin collected' 'Outside'
... ...
I'm not sure if that makes sense, but I appreciate any help regarding that matter!! Thanks in advance!
0 comentarios
Respuesta aceptada
Voss
el 15 de Jul. de 2022
t = table({ ...
'New Scene was loaded: CastleDown'; ...
'Game is trying to save file'; ...
'Saving successful.'; ...
'New Game File created successfully.'; ...
'New Scene was loaded: Outside'; ...
'Periodic Save...'; ...
'Coin collected'; ...
},'VariableNames',{'Status'})
is_new = startsWith(t.Status,'New Scene was loaded:');
C = split(t.Status(is_new),':')
t.Status(is_new) = strcat(C(:,1),':')
scene_name = repmat({''},height(t),1);
scene_name(is_new) = strtrim(C(:,2));
scene_name = fillmissing(scene_name,'previous');
t.Scene = scene_name
2 comentarios
Más respuestas (1)
Campion Loong
el 20 de Jul. de 2022
Editada: Campion Loong
el 20 de Jul. de 2022
Good code indeed. This is almost the same code, but you could go deeper into string with missing handling in the Data Preprocessing ecosystem.
Simply:
t = table([ ...
"New Scene was loaded: CastleDown"; ...
"Game is trying to save file"; ...
"Saving successful."; ...
"New Game File created successfully."; ...
"New Scene was loaded: Outside"; ...
"Periodic Save..."; ...
"Coin collected"] ...
,'VariableNames',"Status");
t.Scence = extractAfter(t.Status,"New Scene was loaded: ");
t.Scence = fillmissing(t.Scence,'previous');
t.Status(startsWith(t.Status,"New Scene was loaded:")) = "New Scence was loaded:"
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!