MATLAB: Unassigned variables and accessing them from outside the function

functionvariable

I have this code:
function [zr,w,R,q,mat] = test(z)
lambda = 1;
w0 = 1;
zr = (pi*w0^2)/lambda;
w = w0*sqrt(1+(z/zr)^2);
R = z + zr^2/z;
q = z + 1i*zr;
mat = [1 z; 0 1];
end
but I not want to have to assign lambda and w0 ( or even z for that matter ), i want them to just stay variables UNLESS I assign them … This is because often it's easier to interpret one variable in terms of the other instead of just having some number… so inputting
[zr,w,R,q,mat] = test(z)
should return the variables exactly how they look inside the function:
zr =
(pi*w0^2)/lambda
w =
w0*sqrt(1+(z/zr)^2)
R =
z + zr^2/z
q =
z + 1i*zr
mat =
1 z
0 1
also, how come these don't exist outside of the function? I can't call q anymore for example after executing test because it doesn't appear to exist anymore.
EDIT:
assigning them as global worked as far as keeping the values assigned by the function :] yay! i don't know if that's the proper way, but it worked XD … still not sure how to return unassigned variable tho …

Best Answer

If you have the symbolic toolbox, use it.
syms lambda phi
zr = (sym('pi')*w0^2)/lambda;