It is legal to change the size of a matrix inside a loop, including being legal to let it grow one element at a time every iteration. However, it is more efficient if you can create the matrix at full size ahead of time. Sometimes much more efficient.
The problem with your code is that you do not initialize the variable Xi but you have
Xi(2:p(i),:) = Xi(1:p(i),:);
That requires that rows 1 to Pmax and all columns of Xi are initialized before the statement is executed -- but Xi is undefined here.
Also, the right hand side has p(i)-1+1 = p(i) rows, but the left hand side has p(i)-2+1 = p(i)-1 rows . This is a mismatch: you would be trying to store 4 rows of data into a location that only holds three rows.