Extracting specific words from cells within a table column and moving them to new column
6 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 15 Jul 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
More Answers (1)
Campion Loong
on 20 Jul 2022
Edited: Campion Loong
on 20 Jul 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 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!