Find and trim text in a table

6 Ansichten (letzte 30 Tage)
Bryan Wilson
Bryan Wilson am 5 Dez. 2017
Beantwortet: Peter Perkins am 19 Dez. 2017
I have a table with a column of strings. In each string, I need to find the '_' character and trim off it and all text to the right. My brain has stopped working and can't figure it out. Thanks!
T = table({"A_1";"A_2";"A_10"},[1;2;1],[6;7;5],...
'VariableNames',{'Section' 'Data1' 'Data2'})
T =
Section Data1 Data2
_______ _____ _____
"A_1" 1 6
"AB_2" 2 7
"B_10" 1 5
What I'd like is....T=
Section Data1 Data2
_______ _____ _____
"A" 1 6
"AB" 2 7
"B" 1 5

Akzeptierte Antwort

KL
KL am 5 Dez. 2017
Bearbeitet: KL am 5 Dez. 2017
use regexprep,
T = table({"A_1";"AB_2";"B_10"},[1;2;1],[6;7;5],...
'VariableNames',{'Section' 'Data1' 'Data2'});
T.Section = cellfun(@(x) regexprep(x,'(?=\_)[\S]*',''),T.Section,'uni',0)
T =
3×3 table
Section Data1 Data2
_______ _____ _____
["A" ] 1 6
["AB"] 2 7
["B" ] 1 5
  1 Kommentar
Bryan Wilson
Bryan Wilson am 5 Dez. 2017
Thank you! As a side note, the regexprep didn't work on my string data, but was just find after a cellstr conversion.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Peter Perkins
Peter Perkins am 19 Dez. 2017
As KL's answer indicated, this isn't really a question about tables, you just need to figure out how to do it for a string column vector, and then put "T." in from of the string variable's name.
If you have strings data, not a cellstr, take a look at extractBefore. I think it does exactly what you're looking for. And
methods string
will lead you to a bunch of other related operations.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by