Search code examples
matlabsymbolsstrsplit

Matlab strsplit at non-keyboard characters


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?


Solution

  • 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] );