Extracting numerical data from a char vector
6 views (last 30 days)
Show older comments
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?
Answers (1)
Voss
on 31 Mar 2022
% 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)
% getting the values back out using regexp:
vals = regexp(str,'([-\.\d]+)','tokens');
vals = str2double([vals{:}])
recovered_lat = vals(1:3:end);
recovered_lon = vals(2:3:end);
recovered_heading = vals(3:3:end);
0 Comments
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!