MATLAB: Help with extracting part of a string

regexpstringstringsvector

I have the string "2DVIS_data_08_40fs", and I want to extract the numbers included between the last underscore '_' and the last two letters 'fs', in this case 40. Given the string "2DVIS_data_08_120fs", I want as output 120 and so on. How can I do this? thanks

Best Answer

Guillame's answer is best, and it's probably good to learn how to use the power of regexp. But if you want a more intuitive-for-beginners approach, find the indices of the underscores, find the indices of fs, and then return everything between them:
somestring = '2DVIS_data_08_40fs';
underscore_indices = strfind(somestring,'_');
fs_indices = strfind(somestring,'fs');
yourNumber = str2double(somestring(underscore_indices(end)+1:fs_indices(end)-1))