JohnnyF
JohnnyF

Reputation: 1101

MATLAB reading all numbers from a string

I'm trying to read an unknown number of numeric values from a string.

For example:

line = 'bla bal bli : 3 5 12 15 266'

I tried sscanf but it requires I know how many numbers are in the string ahead of time.

Upvotes: 0

Views: 2502

Answers (5)

matlabbit
matlabbit

Reputation: 706

Starting in 16b you can use the string class

line    = "bla bal bli : 3 5 12 15 266";
numbers = extractAfter(line,': ');
numbers = split(numbers);
numbers = double(numbers);

This seems to be the fastest option.

>> profFunc
Elapsed time is 0.088259 seconds.
Elapsed time is 1.558718 seconds.
Elapsed time is 1.482743 seconds.

function profFunc

    n = 1E4;

    tic;
        for i = 1:n
            line = "bla bal bli : 3 5 12 15 266";
            numbers = extractAfter(line,': ');
            numbers = split(numbers);
            numbers = double(numbers);
        end
    toc

    tic;
        for i = 1:n
            line = 'bla bal bli : 3 5 12 15 266';
            numbers = str2double(regexp(line,'(?<!\d)(\d)+(?!\d)','match'));
        end
    toc

    tic;
        for i = 1:n
            line = 'bla bal bli : 3 5 12 15 266';
            numstr = strsplit(line,':');
            nums = sscanf(numstr{2},'%u');
        end
    toc
end

Upvotes: 1

nalyd88
nalyd88

Reputation: 5138

Just in case there is the possibility of negative or decimal numbers, the regular expression can be updated as such:

str2double(regexp(line,'(-?\d+\.?\d?)','match'))

Examples:

str2double(regexp('bla bal bli : 3 5 12 15 266','(-?\d+\.?\d?)','match'))

ans =

3 5 12 15 266

str2double(regexp('bla bal bli : 3 5 12 15.4 -266','(-?\d+\.?\d?)','match'))

ans =

3.0000 5.0000 12.0000 15.4000 -266.0000

Upvotes: 0

horchler
horchler

Reputation: 18484

If your strings always have a separator like the colon (':') between the letters and the numbers, you can use strsplit to extract the part of the string with just the numbers, which will be faster than using regular expressions:

line = 'bla bal bli : 3 5 12 15 266';
numstr = strsplit(line,':');

Then you can use sscanf to extract the digits as an array of doubles:

nums = sscanf(numstr{2},'%u') % Assuming digits are always unsigned, if not use %d

If you're using R2012b or older, you won't have strsplit. See this answer for alternatives.

Upvotes: 1

Luis Mendo
Luis Mendo

Reputation: 112689

If you want to extract integer numbers from the strings, you can use a regular expression:

  1. To get the numbers as a cell array of strings:

    >> line = 'bla bal bli : 3 5 12 15 266';
    >> regexp(line,'(?<!\d)(\d)+(?!\d)','match')
    ans = 
        '3'    '5'    '12'    '15'    '266'
    
  2. To get the numbers as a numeric vector:

    >> str2double(regexp(line,'(?<!\d)(\d)+(?!\d)','match'))
    ans =
         3     5    12    15   266
    

Upvotes: 4

mehmet
mehmet

Reputation: 1631

You can use simply isstrprop.

line = 'bla bal bli : 3 5 12 15 266'
line(isstrprop(line, 'digit'))

You extract numbers from the string:

line =

bla bal bli : 3 5 12 15 266


ans =

351215266

Upvotes: 6

Related Questions