Splitting a String in a table once

4 views (last 30 days)
The table provided in the picture is an example of the one i am working with (main table has over 4600 rows). I want/ need to split the first colum at the first white space. So that the output would be "ACTB" and "diff1 day 30" for example.
My internet research has brought me to the function
regexp()
with the parameters: (data,' ','split','once')
data = nameofthetable.columname
So far this hasn't worked for me, since i get the following error:
Error using regexp
The 'STRING' input must be either a char row vector, a cell array of char row vectors, or a
string array.
My question is can i even use this function with a table enviroment and if so how can i make it work and insert a new row? Or is the data type of the first colum wrong and not even a string?
I hope u can help me with this problem.
Thanks in advance.

Accepted Answer

Walter Roberson
Walter Roberson on 4 Oct 2022
I can tell from the way that the table displays that the entries are categorical() not character or string(). You would need to string() the variable before you can regex
  3 Comments
Walter Roberson
Walter Roberson on 6 Oct 2022
S = ["ACTB (rep 1)"; "ACTB ipsc"];
CS = categorical(S);
T = table(S, CS)
T = 2×2 table
S CS ______________ ____________ "ACTB (rep 1)" ACTB (rep 1) "ACTB ipsc" ACTB ipsc
Notice that inside tables, string() objects have the "" decoration around the entries, but categorical() objects have no decoration.
back = string(T.CS)
back = 2×1 string array
"ACTB (rep 1)" "ACTB ipsc"
splits = regexp(back, '\s+', 'split', 'once')
splits = 2×1 cell array
{["ACTB" "(rep 1)"]} {["ACTB" "ipsc" ]}
You do not string() the table, you string() a variable in the table, and the result is a string array. You can insert the results into the table:
variety = categorical(cellfun(@(S) S(:,1), splits));
details = cellfun(@(S) S(:,2), splits);
T.variety = variety;
T.details = details;
T
T = 2×4 table
S CS variety details ______________ ____________ _______ _________ "ACTB (rep 1)" ACTB (rep 1) ACTB "(rep 1)" "ACTB ipsc" ACTB ipsc ACTB "ipsc"

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!