I'm trying to extract numerical data from a character vector using regexp. The character vector data that I'm interested in is
Latitude: XXX.XXXX
Longitude: XXX.XXXX
Heading: XXX.XXXX
where X represents a digit. Each of the numerical data entries can be 2 or 3 digits to the left of the decimal place and 3 or 4 digits to the right of the decimal place. Although the character vector is a row vector, I believe that the latitude, longitude, and heading entries are separated by line breaks.
How can I extract the latitude, longitude, and heading data and place them into their own individual latitude, longitude, and heading vectors of doubles?

1 件のコメント

Stephen23
Stephen23 2022 年 4 月 1 日
@David Zhao: please upload a sample file by clicking the paperclip button.

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

回答 (1 件)

Voss
Voss 2022 年 3 月 31 日

0 投票

% making up a character vector as described:
lat = 40.23;
lon = -32.674;
heading = 66.96;
str = sprintf('%8.4f\n%8.4f\n%8.4f',lat,lon,heading)
str =
' 40.2300 -32.6740 66.9600'
% getting the values back out using regexp:
vals = regexp(str,'([-\.\d]+)','tokens');
vals = str2double([vals{:}])
vals = 1×3
40.2300 -32.6740 66.9600
recovered_lat = vals(1:3:end);
recovered_lon = vals(2:3:end);
recovered_heading = vals(3:3:end);

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 3 月 31 日

コメント済み:

2022 年 4 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by