Extracting specific words from cells within a table column and moving them to new column

13 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

Voss
Voss am 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'})
t = 7×1 table
Status _______________________________________ {'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' }
is_new = startsWith(t.Status,'New Scene was loaded:');
C = split(t.Status(is_new),':')
C = 2×2 cell array
{'New Scene was loaded'} {' CastleDown'} {'New Scene was loaded'} {' Outside' }
t.Status(is_new) = strcat(C(:,1),':')
t = 7×1 table
Status _______________________________________ {'New Scene was loaded:' } {'Game is trying to save file' } {'Saving successful.' } {'New Game File created successfully.'} {'New Scene was loaded:' } {'Periodic Save...' } {'Coin collected' }
scene_name = repmat({''},height(t),1);
scene_name(is_new) = strtrim(C(:,2));
scene_name = fillmissing(scene_name,'previous');
t.Scene = scene_name
t = 7×2 table
Status Scene _______________________________________ ______________ {'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' } {'Periodic Save...' } {'Outside' } {'Coin collected' } {'Outside' }

Weitere Antworten (1)

Campion Loong
Campion Loong am 20 Jul. 2022
Bearbeitet: Campion Loong am 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:"
t = 7×2 table
Status Scence _____________________________________ ____________ "New Scence was loaded:" "CastleDown" "Game is trying to save file" "CastleDown" "Saving successful." "CastleDown" "New Game File created successfully." "CastleDown" "New Scence was loaded:" "Outside" "Periodic Save..." "Outside" "Coin collected" "Outside"

Kategorien

Mehr zu Video games finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by