Transform content of cell array to strings

4 visualizzazioni (ultimi 30 giorni)
Oliver
Oliver il 20 Lug 2016
Commentato: Oliver il 20 Lug 2016
Hi,
I am importing some data, amongst other coordinate points (Lon/Lat), from a xlsx file using readtable. I then use table2array on some columns in order to get rid off the "table type" and be able to use the data for further analysis. The table2array transformation produces a 289x1 cell. Each cell contains only one line such as e.g.: 013° 39.541'E
This coordinate point I want to transform from degrees, minutes to a decimal coordinate using
LonHHMM = sscanf(CheckS, '%d %*s %f');
LonDeg = LonHHMM(1)+LonHHMM(2)/60;
The problem I am having is that I do not know how to extract the content of each cell as string. I do know that for a single cell I can use
CheckS = CPLon{1};
and then the above code to decimal format works fine. But how can I do this for all 289 cells? I tried using cellfun but I dont know what the first argument (@???) should look like. Or do I need to use a for loop in this case?
Your help is highly appreciated. Thanks in advance!
Oliver

Risposta accettata

Thorsten
Thorsten il 20 Lug 2016
Modificato: Thorsten il 20 Lug 2016
cellfun is your friend here. The first argument of cellfun is an anonymous function of one argument, named c. cellfun then passes the content of each cell of your array to this function in the variable c.
(You could also replace c with CheckS, to match the naming in your post.)
X = cell2mat(cellfun(@(c) sscanf(c, '%d %*s %f'), CPLon, 'UniformOutput', false));
LonDeg = X(1,:) + X(2,:)/60
  3 Commenti
Thorsten
Thorsten il 20 Lug 2016
This is probably due to the fact that cellfun creates cells and requires the 'UniformOutput', false parameters, to work, although the output is uniform in this case, namely just two scalar values, which can be converted to a matrix with mat2cell. But this causes overhead which makes the cellfun solution slower.
But if you a big fan of cellfun you may still prefer the concise cellfun syntax, if the drop in performance is not too bad.
Stephen23
Stephen23 il 20 Lug 2016
Modificato: Stephen23 il 20 Lug 2016
' "for loop" solution runs faster than the cellfun code'
cellfun can be much more convenient than a loop, and sometimes it is much tidier code too. But loops are often faster.

Accedi per commentare.

Più risposte (1)

Robert
Robert il 20 Lug 2016
If all the lines of your data take the format of your example line, you could use
x = regexp(myCellArray,'([\d.]*)','match')
x = vertcat(x{:})
x = str2double(x)
to extract the numeric values of your strings.
Then, if your coordinates aren't all East, you will need to grab the direction and apply that to your data as required by your application. You could try
directions = cellfun(@(s)s(end),myCellArray)
If you are able to provide an example (small-ish) data set, we might be able to determine a cleaner way to load your data from the table.
  1 Commento
Oliver
Oliver il 20 Lug 2016
Hi Robert,
I attached a small data set from the xlsx file that I can share with you. Always looking for ways to improve my code :) Thanks!

Accedi per commentare.

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by