I have a column in my table that has values such as '2.6ELKUxQKWPVJVHxxxxC.4xxxIxSxJJxxxxx', it has altogether 37 single characters. I want to split the string into 37 different columns for further data analysis. I have tried using 'split' function, but it doesn't work.

1 commentaire

Stephen23
Stephen23 le 17 Mai 2018
Modifié(e) : Stephen23 le 17 Mai 2018
"I want to split the string into 37 different columns..."
Your char vector already has 37 columns. This is easy to check:
>> str = '2.6ELKUxQKWPVJVHxxxxC.4xxxIxSxJJxxxxx';
>> size(str)
ans =
1 37

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 17 Mai 2018
Here's an example for 8 columns. You can expand it to 37 columns.
ca = {'1.6ELKUxQKWPVJVHxxxxC.4xxxIxSxJJxxxxx'; '2.6ELKUxQKWPVJVHxxxxC.4xxxIxSxJJxxxxx'; '3.6ELKUxQKWPVJVHxxxxC.4xxxIxSxJJxxxxx'}
t = cell2table(ca)
% Now we have our table.
% Extract this one column:
col1 = t{:,1}
% Make 37 cell arrays
ca1 = cell(37, 1);
ca2 = cell(37, 1);
ca3 = cell(37, 1);
ca4 = cell(37, 1);
ca5 = cell(37, 1);
ca6 = cell(37, 1);
ca7 = cell(37, 1);
ca8 = cell(37, 1);
for row = 1 : length(col1)
thisRow = col1{row};
ca1{row} = thisRow(1);
ca2{row} = thisRow(2);
ca3{row} = thisRow(3);
ca4{row} = thisRow(4);
ca5{row} = thisRow(5);
ca6{row} = thisRow(6);
ca7{row} = thisRow(7);
ca8{row} = thisRow(8);
end
t2 = table(ca1, ca2, ca3, ca4, ca5, ca6, ca7, ca8)

3 commentaires

Here's an example for 8 columns. You can expand it to 37 columns.
Eww! I'm sorry but no. Numbered variables are an abomination. In addition there seems to be a mix-up between rows and columns since t2 ends up as 37 rows x 8 columns table.
I stand by my initial statement that the whole exercise is completely pointless. It's trivial to index character of a char array so adding an unnecessary extra level of indexing is a waste of time. I certainly don't see the point in stuffing individual characters as columns of a table. Tables are designed to store heterogeneous data, not this.
But if it's really what's needed, then
t2 = array2table(cell2mat(t{:, 1}))
is a lot simpler than this looping and numbered variables.
Abdul Rehan Khan Mohammed
Abdul Rehan Khan Mohammed le 18 Mai 2018
Modifié(e) : Abdul Rehan Khan Mohammed le 18 Mai 2018
Thanks! yes, it is a lot simpler.
Image Analyst
Image Analyst le 18 Mai 2018
Yes simpler, but I agree with Guillaume that it's better to leave it as a string and not create a table with all those columns.

Connectez-vous pour commenter.

Plus de réponses (2)

KSSV
KSSV le 17 Mai 2018
str = '2.6ELKUxQKWPVJVHxxxxC.4xxxIxSxJJxxxxx' ;
iwant = cell(1,length(str)) ;
for i = 1:length(str)
iwant{i} = str(i) ;
end

3 commentaires

Stephen23
Stephen23 le 17 Mai 2018
Modifié(e) : Stephen23 le 17 Mai 2018
Much simpler to use num2cell:
iwant = num2cell(str);
KSSV
KSSV le 17 Mai 2018
Modifié(e) : KSSV le 17 Mai 2018
Yaaaa..:/
Guillaume
Guillaume le 17 Mai 2018
And even much simpler is not to bother at all. str already has 37 different columns. Each one can be accessed with str(columnindex).

Connectez-vous pour commenter.

Guillaume
Guillaume le 17 Mai 2018
A char array such as
str = '2.6ELKUxQKWPVJVHxxxxC.4xxxIxSxJJxxxxx'
already has different columns. If you want to access column 6 of str, it's simply:
str(6)
Exactly the same as when accessing columns of a numerical matrix.

1 commentaire

Image Analyst
Image Analyst le 17 Mai 2018
He means columns of his table, not columns of that string.

Connectez-vous pour commenter.

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by