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 comentario

Stephen23
Stephen23 el 1 de Abr. de 2022
@David Zhao: please upload a sample file by clicking the paperclip button.

Iniciar sesión para comentar.

Respuestas (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)
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);

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Productos

Versión

R2021b

Preguntada:

el 31 de Mzo. de 2022

Comentada:

el 1 de Abr. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by