Extracting numerical data from a char vector
显示 更早的评论
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
2022-4-1
回答(1 个)
% 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);
类别
在 帮助中心 和 File Exchange 中查找有关 Variables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!