Question

I have lat and lon as cells of size 1x7 (in which inside each cell, the cell is a column with varying numbers of rows). I want to convert each of the 7 columns to double and then concatenate them into a column so that I have a matrix of size 216x1.

For example (shortened version), if

 lat{1,1}
'40.400959'
'40.695078'
'40.969112'
'41.264171'
'41.400968'
'41.517772'
'41.530011'
'41.550005'
'41.583183'
'41.603159'

 lat{1,2}
'38.082155'
'38.176278'
'38.529631'
'38.612034'
'38.700632'

Then, I want to have, in double form: 40.400959 40.695078 40.969112 41.264171 41.400968 41.517772 41.530011 41.550005 41.583183 41.603159 38.082155 38.176278 38.529631 38.612034 38.700632

I managed to convert to double, but I'm not sure how to do the concatenate (or reshape) to column part.

for i = 1:7
    lat = str2double(lat_PM25{i});
    lon = str2double(lon_PM25{i});
end
Was it helpful?

Solution

Use this:

str2double(vertcat(lat{:}))

How this works:

lat{:} gives you a comma-separated list of the cell contents. Each cell content is in turn a single-column cell-array of strings (I assume).

vertcat concatenates all those single-column cell-arrays of strings into one single-column cell-array of strings.

str2double converts each string into a double number and packs all those numbers into a column vector.

Example:

lat{1,1} = {
'40.400959'
'40.695078'
'40.969112'
'41.264171'
'41.400968'
'41.517772'
'41.530011'
'41.550005'
'41.583183'
'41.603159'};

lat{1,2} = {
'38.082155'
'38.176278'
'38.529631'
'38.612034'
'38.700632'};

gives

ans =
   40.4010
   40.6951
   40.9691
   41.2642
   41.4010
   41.5178
   41.5300
   41.5500
   41.5832
   41.6032
   38.0822
   38.1763
   38.5296
   38.6120
   38.7006
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top