MATLAB: I’m not sure why what I wrote isn’t working in terms of extracting the string I want.

extracting strings

I have:
filename = Synch_ab001.mat;
underscore_indices = strfind(filename, '-');
period_indices = strfind(filename, '.');
SubjectName = strtok(filename(underscore_indices(end)+1:period_indices(end)-1));
I wrote this to extract just the ab001 as SubjectName, but I keep getting a "subscript indices must either be real positive integers or logicals."

Best Answer

filename = 'Synch_ab001.mat';
subjectname = regexp(filename,'(?<=_)(.*)(?=\.\w*)','match')