How to split a table column into two seperate columns

I have a table which looks like this:
I want to split column a into two seperate columns from the delimiter "\" such that first column will be like 'AIVA-100' and the other column will be '01_Test.....'.
I want to do this for every element in column a. In the end, i will have 2 columns with the elements of column a.
I tried to iterate through the table and split using the delimiter but i couldn't do it

 Réponse acceptée

The regexp function works with cell arrays easily —
C = {'AIVA-100\something'; 'AIVA-75\something else'};
Cs = regexp(C,'\','split')
Cs = 2×1 cell array
{1×2 cell} {1×2 cell}
Cs{1}
ans = 1×2 cell array
{'AIVA-100'} {'something'}
Cs{2}
ans = 1×2 cell array
{'AIVA-75'} {'something else'}
Cs = cellfun(@(x)split(x,'\'), C, 'Unif',0) % The 'split' Function Requires 'cellfun'
Cs = 2×1 cell array
{2×1 cell} {2×1 cell}
Cs{1}
ans = 2×1 cell array
{'AIVA-100' } {'something'}
Cs{2}
ans = 2×1 cell array
{'AIVA-75' } {'something else'}
.

4 commentaires

I think my problem here is that it's not a cell array, rather cells. I need to convert it somehow to a cell array first.
I am a bit confused as to what ‘a’ actually is.
Perhaps —
a{1,:} = {'AIVA-100\something'};
a{2,:} = {'AIVA-75\something else'};
a
a = 2×1 cell array
{1×1 cell} {1×1 cell}
asplit = regexp([a{:}],'\','split') % Use The 'regexp' Function
asplit = 1×2 cell array
{1×2 cell} {1×2 cell}
asplit{1}
ans = 1×2 cell array
{'AIVA-100'} {'something'}
asplit{2}
ans = 1×2 cell array
{'AIVA-75'} {'something else'}
asplit = cellfun(@(x)split(x,'\'), a, 'Unif',0) % Use The 'split' Function With 'cellfun'
asplit = 2×1 cell array
{2×1 cell} {2×1 cell}
asplit{1}
ans = 2×1 cell array
{'AIVA-100' } {'something'}
asplit{2}
ans = 2×1 cell array
{'AIVA-75' } {'something else'}
.
Worked like a charm. Thank you for your help!
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (2)

y = {'AIVA_1\abc';'AIVA_12\def';'AIVA_123\xyz'}
y = 3×1 cell array
{'AIVA_1\abc' } {'AIVA_12\def' } {'AIVA_123\xyz'}
z=split(y,'\')
z = 3×2 cell array
{'AIVA_1' } {'abc'} {'AIVA_12' } {'def'} {'AIVA_123'} {'xyz'}
column1 = z(:,1)
column1 = 3×1 cell array
{'AIVA_1' } {'AIVA_12' } {'AIVA_123'}
column2 = z(:,2)
column2 = 3×1 cell array
{'abc'} {'def'} {'xyz'}

1 commentaire

Firstly, thank you for your reply.
The elements in column a are cells and not a cell array, hence i cannot use the split function for those. Do you know how can i convert those cells into a cell array?

Connectez-vous pour commenter.

Not super elegant, but you could use something like:
t = table() % your table
% Split by tokens to grab the first part of a and put it back into a, put
% the remainder in new_a
[t{:,'a'},t{:,'new_a'}] = strtok(t{:,'a'},'\');
% Get rid of the leading delimiter in new_a and put the results back into
% new_a
t{;,'new_a'} = strtok(t{:,'new_a'},'\');

Catégories

En savoir plus sur Variables dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by