How to append 3 dimension array( matrix) for example in MATLAB M(:,:,i) = b

To handle concatenation of empty matrices you may need to define a user function. Something like cat2() below, just to give an idea:

cat2(A,B) = isempty(B) ? A : isempty(A) ? B : [A;;;;B]