Unassigned variables and accessing them from outside the function

9 views (last 30 days)
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 ...
  2 Comments
Image Analyst
Image Analyst on 17 May 2015
That's only okay (making them global) if you don't accept them as input arguments, or return them as output arguments. Otherwise an error will say something like "zr might be used incompatibly or redefined?
If values are not assigned, then they don't even exist and you can't return them. If you have arguments in the output arg list, and you did not assign them, then when you call the function it will say something like "Output argument "q" (and maybe others) not assigned during call"
Solarmew
Solarmew on 17 May 2015
Values are assigned tho. They are assigned inside the function. I don't actually need the function to return all of them, but I want all of them to exist AFTER the function is done executing. So if I feed it some "z" value, it'll come up with some "q" value, but after it does all the calculations with that "q" it seems to erase whatever value was assigned to it. I don't want that. I want that value to still be there available for use by other functions that I will have later.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 May 2015
If you have the symbolic toolbox, use it.
syms lambda phi
zr = (sym('pi')*w0^2)/lambda;
  2 Comments
Solarmew
Solarmew on 17 May 2015
Edited: Solarmew on 17 May 2015
Thanks, that appears to do half the job. Now, if I wanted to actually assign lambda something (presumably before or when calling the function), how do I do so without going to the function and removing it from the syms list? Or do you think it would be easier to just put all variables in syms to begin with and then
Walter Roberson
Walter Roberson on 18 May 2015
Example:
function zr = test(z, given_lambda, given_w0)
syms lambda w0
zr = (sym('pi')*w0^2)/lambda;
if exists('given_lambda', 'var')
zr = subs(zr, lambda, given_lambda);
end
if exists('given_w0', 'var')
zr = subs(zr, w0, given_w0);
end
if isempty(symvar(zr)) %no remaining free variables?
zr = double(zr); %convert it to double
end
This is generalized, in that the code code after the function line could be used anywhere in a routine, as long as the names to look for were known. I did not code in terms of nargin or make any assumption about the order of parameters. Code can be made more compact if you can make assumptions that your values are coming from optional parameters whose order is known. With the code I gave here, though, you could do things such as
if get(handles.use_default_lambda,'Value', 1) %pushbutton is clicked
use_lambda = 1.234;
end
That is, you could have code paths that define the variable or not, and then later test to see if the variable was defined before doing the subs().
I probably wouldn't use this code myself, but for more subtle reasons beyond the reach of this discussion.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 17 May 2015
They DO exist outside the function. When, in the main (calling) function, you do this:
[zr,w,R,q,mat] = test(z)
that means that zr, w, R, q, and mat will have the same values in your calling function that they had inside the test() function (because you used the same names for them).
I do not understand your desire not to pass in any "z" value to test(). What's the point of having input variables then? If you want z to be a constant, just assign it inside test.
I do not understand the need to avoid assigning lambda and w0 inside the function. What's the big deal? What's wrong with doing that? Just how do you expect them to have a value if you don't assign one???
  1 Comment
Solarmew
Solarmew on 17 May 2015
Edited: Solarmew on 17 May 2015
i guess you can disregard the comment about not assigning z ... I'm just used to Mathematica ... but as far as Lambda and w0, I will have really long and complicated functions involving them later on and I want to be able to easily check my results. If lambda = 1476187691.25382658275 and w0 = sqrt(12412.961286), maybe I want to check that the function returned actually goes like lambda^(4/5)*exp(w0) instead of getting some giant number and trying to figure out if it's really of the right form

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!