tytamu
tytamu

Reputation: 565

how to get the number from a string in matlab

I want to how know to get certain numbers from a string in matlab. For example, I have a string:

'ABCD_01 36_00 3 .txt', (there is spacing between 01 and 36)

What I need is to get the number 36 and 3. How can I do it in matlab? I've tried finding the answer from previous posts but can not find one that fits this purpose. Thanks for the help.

Upvotes: 4

Views: 18599

Answers (2)

Amro
Amro

Reputation: 124563

Regular expressions:

>> str = 'ABCD_01 36_00 3 .txt';
>> t = str2double( regexp(str,'.* (\d+)_.* (\d+)','tokens','once') )

t =
    36     3

Upvotes: 10

b3.
b3.

Reputation: 7165

If the filenames always start with four characters you can do:

>> filename = 'ABCD_01 36_00 3 .txt';
>> sscanf(filename, '%*4c_%*u %u_%*u %u.txt')

ans =

    36
     3

Upvotes: 6

Related Questions