arraysmatlabtype-conversioncell-array

Turning a matlab string with a numbers and colon to an array populated using colon


If I have some string newStr = 1×1 cell array

{'1 25 27 45 46 62 65 70 73 76 77:83'}

Is there any way to directly convert this to an array?

my_array = [1 25 27 45 46 62 65 70 73 76 77:83];

which would result in

my_array =

 1    25    27    45    46    62    65    70    73    76    77    78    79    80    81    82    83

Solution

  • You can do this with str2num

    a = {'1 25 27 45 46 62 65 70 73 76 77:83'}
    my_array = str2num(a{1})
    
    my_array =
    
      Columns 1 through 12
    
         1    25    27    45    46    62    65    70    73    76    77    78
    
      Columns 13 through 17
    
        79    80    81    82    83