Splitting columns based on comma
14 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
greenyellow22
il 1 Giu 2022
Commentato: greenyellow22
il 6 Giu 2022
Hey everyone, I have a table (raw) with several columns, one of them is 'Position' (see below) which contains different coordinates. Some rows are blank (''). I want to split the coordinates based on the comma between them and put them seperately back to the 'raw' table.
raw.Position =
''
''
''
'(-6.9, 4.8, 68.4)'
'(-8.5, 3.1, 18.0)'
'(13.5, 2.5, 70.1)'
''
''
I tried the following
P = raw.Position
new = regexp(P, ',','split');
raw.a = P(:,1); Error: Index in position 2 exceeds array bounds. Index must not exceed 1.
raw.b = P (:,2);
raw.c = P (:,3);
but the 'new' table saved the splitted coordinates in 1x3 cells, so they could not be transfered back to the 'raw' table (I think that's the problem). Also, I wanted to remove the brackets as well from the coordinates which did not work either.. Any help appreciated!
new =
''
''
''
1x3 cell
1x3 cell
1x3 cell
''
''
2 Commenti
Image Analyst
il 1 Giu 2022
Make it easy for people to help you. Attach one of the data files or the "raw" variable in a .mat file with the paperclip icon.
Risposta accettata
Stephen23
il 1 Giu 2022
raw.Position = {'';'';'';'(-6.9, 4.8, 68.4)';'(-8.5, 3.1, 18.0)';'(13.5, 2.5, 70.1)';'';''};
tmp = sscanf(sprintf('%s',raw.Position{:}),'(%f,%f,%f)',[3,Inf]).';
raw.a = tmp(:,1);
raw.b = tmp(:,2);
raw.c = tmp(:,3)
3 Commenti
Stephen23
il 3 Giu 2022
aw.Position = {'';'';'';'(-6.9, 4.8, 68.4)';'(-8.5, 3.1, 18.0)';'(13.5, 2.5, 70.1)';'';''};
fnh = @(t) sscanf(t,'(%f,%f,%f)',[1,3]);
V = cellfun(fnh,aw.Position,'uni',0)
Stephen23
il 3 Giu 2022
aw.Position = {'';'';'';'(-6.9, 4.8, 68.4)';'(-8.5, 3.1, 18.0)';'(13.5, 2.5, 70.1)';'';''};
fnh = @(t) sscanf(t,'(%f,%f,%f)',[1,3]);
V = cellfun(fnh,aw.Position,'uni',0);
V(cellfun(@isempty,V)) = {nan(1,3)};
M = vertcat(V{:})
Più risposte (2)
greenyellow22
il 3 Giu 2022
4 Commenti
Stephen23
il 4 Giu 2022
Modificato: Stephen23
il 5 Giu 2022
"Not sure if that makes sense"
Yes, it makes sense. As I already wrote in my earlier comment, the cause is probably that your data differ in some way to the example data that you gave. Because you did not upload your actual data in a mat file by clicking the paperclip button, I cannot investigate this for you. If you do upload your data, then I can investigate it.
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!