In this instance I have a cell array of lat/long coordinates that I am reading from file as strings with format:
x = {'27° 57'' 21.4" N', '7° 34'' 11.1" W'}
where the °
is actually a degree symbol (U+00B0).
I want to use strsplit()
or some equivalent to get out the numerical components, but I don't know how to specify the degree symbol as a delimiter.
I'm hesitant to simply split at the ','
and index out the number, since as demonstrated above I don't know how many digits to expect.
I found elsewhere on the site the following suggestion:
x = regexp(split{1}, '\D+', 'split')
however this also separates the integer and decimal components of the decimal numbers.
Is there a strsplit()
option, or some other equivalent I could use?
You can copy-paste the degree symbol from your data file to your M-file script. MATLAB fully supports Unicode characters in its strings. For example:
strsplit(str, {'°','"',''''})
to split the string at the three symbols.
Alternatively, you could use sscanf
(or fscanf
if reading directly from file) to parse the string:
str = '27° 57'' 21.4"';
dot( sscanf(str, '%f° %f'' %f"'), [1, 1/60, 1/3600] );
Minor technical niggle: Matlab doesn't fully support Unicode characters, only characters in the Basic Multilingual Plane that can be represented with a single UCS-2 code point. Characters like emoji that require UTF-16 surrogate pairs are not supported. (I'm not saying this to be pedantic; I actually want to use emoji sometimes and run in to this as a real use case.)
@AndrewJanke: I was not aware of that, thanks for the clarification!