fun = matlabFunction(q,'Vars',[ph,phi,r,z]);
z=linspace(0.01,10);
That is within a loop. The first time sym z is in effect but immediately after you replace z with numeric z. The second time through z is no longer symbolic and so cannot be used in 'Vars'