Transform content of cell array to strings
2 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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
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.
その他の回答 (1 件)
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.
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!