JJ1603
JJ1603

Reputation: 636

Matlab split text column in a table

I have a table object in MatLab with a text column. This text column is a "tag" and contains underscores two split the tag.

I'd like to create a column with the second element of the tag. I used strsplit but It didn't work. Also I tried regexp but it gives me a cell object with 126 cells objects inside, and I don't know how to extract the second element of every cell.

Any suggestion?

Example:

a = {'a_b'; 'a_c';'a_n';'a_t'}
t = table(a)

I just want a vector with the second element. Thanks.

Upvotes: 0

Views: 388

Answers (1)

Finn
Finn

Reputation: 2343

How about

t=[t rowfun(@(x) x{1}(3),t)]

with 1 being the column and 3 being the element you want. For undefined length of the string parts it gets a little bit more tricky

t=[t rowfun(@(X) X{1}(strfind(X{1},'_')+1:end),t,'OutputFormat','cell')];

strfind() gets the '_' element so (find+1:end) is the rest of the string. as they can be of different length everything has to a cell as Output and then be added to the table. if the column changes you have to adopt the code in both {1}

Upvotes: 2

Related Questions