Parsing table of string without looping

조회 수: 1 (최근 30일)
PsykotropyK
PsykotropyK 2020년 2월 1일
편집: Stephen23 2020년 2월 2일
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
Guillaume
Guillaume 2020년 2월 1일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2020년 2월 1일
편집: Stephen23 2020년 2월 1일
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
PsykotropyK
PsykotropyK 2020년 2월 1일
편집: PsykotropyK 2020년 2월 2일
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 2020년 2월 2일
편집: Stephen23 2020년 2월 2일
@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 '

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by