MATLAB: File name manipulation

strings

I have a file name that takes the form: S_1_X_Y where X can be an integer between 1-16, and Y is a letter a,b,c,d.
I want to be able to just replace the number X by a user defined value.
any help greatly appreciated. Thanks

Best Answer

X = 1;
Y = 'a';
sprintf('S_1_%d_%s',X,Y)
EDIT
str = 'S_1_6_a'
x = 3;
regexprep(str,'(_)\d+(_[abcd])', ['$1' sprintf('%d',x) '$2'])
ans =
S_1_3_a