Filter löschen
Filter löschen

Read nonrectangle csv with brackets

2 Ansichten (letzte 30 Tage)
stephen
stephen am 15 Sep. 2015
Bearbeitet: per isakson am 16 Sep. 2015
I have a csv file that contains pairs of latitudes and longitudes and other types of data. For example, the first two lines after the headers are
"T1","B",NA,15,20000542,1408039037,"A","False","[[-8.585676, 41.148522], [-8.585712000000001, 41.148638999999996], [-8.585685000000002, 41.148855000000005], [-8.585730000000002, 41.14892699999999], [-8.585982, 41.148962999999995], [-8.586396, 41.148954], [-8.586072, 41.14872], [-8.586324000000001, 41.147847], [-8.586999, 41.147459999999995], [-8.586575999999999, 41.14715400000001], [-8.584883999999999, 41.146623000000005]]"
and
"T2","B",NA,57,20000108,1408038611,"A","False","[[-8.610876000000001, 41.14557], [-8.610858, 41.145579000000005], [-8.610903, 41.145768], [-8.610444, 41.146190999999995], [-8.609445000000001, 41.146758], [-8.608896, 41.147118], [-8.608968, 41.147127], [-8.608707, 41.147532000000005], [-8.608347, 41.148117000000006], [-8.608149, 41.148351000000005], [-8.608041, 41.148576000000006], [-8.607654, 41.14926], [-8.607348000000002, 41.149899000000005], [-8.607393, 41.149899000000005], [-8.607357, 41.149962], [-8.606817, 41.150979], [-8.606358, 41.151914999999995], [-8.605719, 41.152788], [-8.604981, 41.153318999999996], [-8.604783, 41.154345], [-8.604828, 41.154372], [-8.604801, 41.155353], [-8.604648000000001, 41.156774999999996], [-8.604522, 41.158197], [-8.604513, 41.159943000000005], [-8.604377999999999, 41.16055500000001], [-8.604377999999999, 41.1606], [-8.604369, 41.160644999999995], [-8.60436, 41.160807], [-8.604162, 41.161176], [-8.604126, 41.161248], [-8.60409, 41.16129300000001], [-8.60409, 41.161266000000005], [-8.604108, 41.161239], [-8.604126, 41.161194], [-8.604135, 41.161275], [-8.60391, 41.162048999999996], [-8.602929000000001, 41.162832], [-8.602551000000002, 41.163111], [-8.601894, 41.163597]]"
The strings have quotes and the lat/long column is a string of variable length. I am primarily interested in the lat/long data and would like to read each line into a numeric cell array where the first column is the latitude and the second is the longitude and the rows are the points. For example,
C{1}(1,1) = -8.585676
C{1}(1,2) = 41.148522
C{1}(2,1) = -8.585712000000001,...
C{2}(1,1) = -8.610876000000001, ...
I have tried xlsread but that reads the lat/longs in as strings with the brackets and does not separate into latitude and longitude. What is the best way to do this?
Thanks,

Akzeptierte Antwort

per isakson
per isakson am 16 Sep. 2015
Bearbeitet: per isakson am 16 Sep. 2015
I think reading and parsing must be done in two steps. With R2013a
>> tic, C = cssm( 'cssm.csv' ), toc
C =
[11x2 double]
[40x2 double]
Elapsed time is 0.009517 seconds.
>> C{1,1}(1,:)
ans =
-8.5857 41.1485
>> C{1,1}(2,:)
ans =
-8.5857 41.1486
>>
where
function out = cssm( filespec )
% out = cssm( 'cssm.csv' )
fid = fopen( filespec );
cac = textscan( fid, '%*q%*q%*s%*f%*f%*f%*q%*q%q', 'Delimiter', ',' );
fclose( fid );
buf = regexprep( cac{1}, '\] *\,', '];' ); % replace "]," by "];"
out = cell( size( buf ) );
for jj = 1 : length( buf )
str = buf{jj};
out{jj} = str2num( str );
end
end
and cssm.csv contains the two lines of your question.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by