MATLAB: Extracting numbers from mixed string

strings

I have filenames saved as strings such as '2001_06m'. Sometimes the files are inconsistently named as '2001_6m' (missing the zero before the 6) or '2001_06' (missing the m at the end). What code would I use to extract the non-zero integers after underscore in all cases (i.e. output = 6)?
And separately, what code would I use to extract the numbers before the underscore (usually they are 4 digits long, but sometimes 3 digits, i.e. '001' instead of '2001')?

Best Answer

s = '2001_06m';
d = sscanf(s, '%d_%d')
ans =
2001
6
Easier and faster than regexp.
[EDITED] If the input is a cell string:
C = {'2001_06m', '002_77q'};
S = sprintf('%s ', C{:});
S(S < '0' | S > '9') = ' '; % Mask all non-numbers
Num = sscanf(S, '%d %d ', [2, Inf]);