Parsing table of string without looping
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
PsykotropyK
am 1 Feb. 2020
Bearbeitet: Stephen23
am 2 Feb. 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 Kommentar
Guillaume
am 1 Feb. 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.
Akzeptierte Antwort
Stephen23
am 1 Feb. 2020
Bearbeitet: Stephen23
am 1 Feb. 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 Kommentare
Stephen23
am 2 Feb. 2020
Bearbeitet: Stephen23
am 2 Feb. 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 '
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!