Transform content of cell array to strings

9 ビュー (過去 30 日間)
Oliver
Oliver 2016 年 7 月 20 日
コメント済み: Oliver 2016 年 7 月 20 日
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

採用された回答

Thorsten
Thorsten 2016 年 7 月 20 日
編集済み: Thorsten 2016 年 7 月 20 日
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 件のコメント
Thorsten
Thorsten 2016 年 7 月 20 日
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 2016 年 7 月 20 日
編集済み: Stephen23 2016 年 7 月 20 日
' "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.

サインインしてコメントする。

その他の回答 (1 件)

Robert
Robert 2016 年 7 月 20 日
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 件のコメント
Oliver
Oliver 2016 年 7 月 20 日
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!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by