MATLAB: Extract number from file name

doublenumbernumericstring

There are several files like this: K10_0.0.json, Mig_Thresh_2.0.json, K_5_6.5.json, WC_0.00051.json, … and I need to extract the number after the last underline which would be 0.0 for K10_0.0.json, 2.0 for Mig_Thresh_2.0.json, 6.5 for K_5_6.5.json and 0.00051 for WC_0.00051.json. In other words, I need to get the number after the last underline (_).
Any idea how to do that?

Best Answer

Look at regexp function. I assume your input is a string array and you want a string array output.
a='K_5_6.5.json';
b=regexp(a,'(?<=[_])\d*[.]?\d*','match');
c=cell2mat(b(end));%c='6.5'