Extracting specific words from cells within a table column and moving them to new column
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Voss
2022 年 7 月 15 日
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
その他の回答 (1 件)
Campion Loong
2022 年 7 月 20 日
編集済み: Campion Loong
2022 年 7 月 20 日
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 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!