You never assigned S4 because you never went into the if block, which means len is not greater than len2.
You need to learn how to step through your code to debug it. You won't get far in MATLAB without knowing that. So invest a few minutes here: