First of all, you should format your code properly: https://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question so that others can easily read it.
There are some mistakes in your merge function. You are using vec on some lines instead of vec1 or vec2, so it is difficult for me to understand what do mean by merging two sorted vectors. Still, I have tried to answer this question according to my understanding of this code. You can see it in answer section below.