MATLAB: How to extract only part of a file name

extractfilename

I am trying to extract a file but i don´t want to extract it´s entire name. For example:
In
inv_1000061014358_20150424_115235
I only want to extract inv_1000061014358.
Can anyone help?
Thanks!!!!

Best Answer

Presuming the numeric substring isn't the same length always,
ix=strfind(s,'_'); % get the underscore locations
t=s(1:ix(2)-1); % return the substring up to 2nd underscore
One can also use find to only get the first two locations; would be handy if it also had the ability to return an N th argument instead of just first or last N; then wouldn't have to use the intermediate index vector.
Or, one can use regexp