MATLAB: How to extract variables from a filename

datavariables

I am trying to read variables from a bunch of file names that all have a consistent format like:
filename = string1_useless_float1_useless_float2_ etc, etc
How can I read certain variables from the file name (strings, floating points) while skipping over unimportant text in between? I'm trying to use these variables in another program but I have literally hundreds of thousands of files so doing this by hand is clearly out of the question.
Thank you!

Best Answer

strsplit(filename, '_') and index the resulting cell array.
If you want to get fancier you could use regexp, such as
regexp(filename, '[^_]+(?:_[^_]+_)', 'match')
which would "use up" the string in pairs of fields, not returning the second of each pair. But to be honest, if your first thought was not to use regexp() then chances are you would waste more time trying to get the regular expression to work right; the strsplit() has a huge advantage in simplicity.