Parsing table of string without looping

1 vue (au cours des 30 derniers jours)
PsykotropyK
PsykotropyK le 1 Fév 2020
Modifié(e) : Stephen23 le 2 Fév 2020
Hello,
I have a table of string (name : polystring 300 000 x 1 table) where each string register a set of gps coordinates that defines a polygon.
For instance, one string can be : 2.331878 48.866003, 2.331872 48.866107, 2.331855 48.866211, 2.331825 48.866314, 2.331785 48.866416, 2.331733 48.866517, 2.331669 48.866617, 2.331595 48.866715, 2.331509 48.866811
Each got a different number of points (from a few to 200 points or so).
By using this command :
B = cellfun(@(x) strsplit(x, ", ")', polystring, 'uni', false);
I managed to create an array of cells (300 000 x 1 cell) where each cell is a (N x 1 cell). Each cell is storing a string like "2.331878 48.866003"
I would like now to parse the cells to have a (300 000 x 1) with each cell being a (N x 2) matrix, by parsing my previous (N x 1) using " " as a splitter.
I do not get how I should do it as using
C = cellfun(@(x) str2double(strsplit(x, " ")), B, 'uni', false);
Trigger an error 'First input must be either a character vector or a string scalar.'
Ultimately, the aim is to create polygon (polyshape(P) with P being my Nx2 matrix of GPS coordinates)
A warm thanks for any help or hint provided
  1 commentaire
Guillaume
Guillaume le 1 Fév 2020
Note that if the initial table of strings is the result of a text file import a much more efficient solution would be to fix the import so that it imports the data directly as a numeric array/table. To help with that, we need the details of the text file.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 1 Fév 2020
Modifié(e) : Stephen23 le 1 Fév 2020
Rather than slow and complex string manipulation you can easily use sscanf to directly convert each string into a numeric matrix:
>> str = '2.331878 48.866003, 2.331872 48.866107, 2.331855 48.866211, 2.331825 48.866314, 2.331785 48.866416, 2.331733 48.866517, 2.331669 48.866617, 2.331595 48.866715, 2.331509 48.866811';
>> sscanf(str,'%f%f,',[2,Inf]).'
ans =
2.3319 48.8660
2.3319 48.8661
2.3319 48.8662
2.3318 48.8663
2.3318 48.8664
2.3317 48.8665
2.3317 48.8666
2.3316 48.8667
2.3315 48.8668
Repeat for each cell.
  2 commentaires
PsykotropyK
PsykotropyK le 1 Fév 2020
Modifié(e) : PsykotropyK le 2 Fév 2020
Great thanks, using it with cellfun made the trick :
B = cellfun(@(x) sscanf(x,'%f%f,',[2,Inf]), polystring, 'uni', false);
And ultimately to create the polygons
B = cellfun(@(x) polyshape(sscanf(x,'%f%f,',[2,Inf])'), polystring, 'uni', false);
Stephen23
Stephen23 le 2 Fév 2020
Modifié(e) : Stephen23 le 2 Fév 2020
@PsykotropyK: I hope that it helps. Note that it is a good habit to use a normal element-wise transpose .' unless you specifically need the complex conjugate transpose '

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by