Unassigned variables and accessing them from outside the function
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2015-5-17
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"
采纳的回答
Walter Roberson
2015-5-17
If you have the symbolic toolbox, use it.
syms lambda phi
zr = (sym('pi')*w0^2)/lambda;
2 个评论
Walter Roberson
2015-5-18
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.
更多回答(1 个)
Image Analyst
2015-5-17
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???
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!