Assign a variable from within a function to the script I used to call the function from.
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have ran in to a problem and I have been trying to deal with this for hours and I just don't get it... Help will be extremely appreciated!
The function looks like:
function [A,x] = fd4(L,N)
x = linspace(0,L,N+1)';
x(1) = []; % Remove first point
h = x(2)-x(1);
e = ones(N,1);
A = spdiags([e -4*e 6*e -4*e e], -2:2, N, N);
A(1,1:4) = [16 -9 8/3 -1/4];
A(end-1,end-3:end) = [16/17 -60/17 72/17 -28/17];
A(end,end-3:end) = [-12/17 96/17 -156/17 72/17];
B =0;
A = full(A);
A = A/h^4;
B = cond(A,inf)
assignin(UU2,'D',B)
Basically I have a matrix and through my other script I give the value input for L and N. My issues lies in the very end of the function. After I assign values for L and N, I calculate the cond(A, inv) and assigns this value to the variable B. I would like to transfer this value of B to another variable D (not used before) to the script I used to call the function from. I thought this could be done by assignin(UU2,'D',B) (UU2) is the name of my script where I call the function from. But I keep getting error messages that Im trying to run a script as a function:( help?
Best regards, Christoffer
0 个评论
采纳的回答
Jan
2015-10-4
编辑:Jan
2015-10-4
Please read the documentation of assignin. There you find the explanation, that the 1st input is either 'base' or 'caller'. But you use a function call to the script, which starts this script for an evaluation. The appearing error message should be clear.
Better define an output argument:
function [A, x, B] = fd4(L, N)
...
Then call this function from your script like this:
[A, x, B] = fd4(L, N);
Then B is assigned as wanted without brute tricks as assignin, which impedes debugging due to magic remote creation of variables. A reader of your script cannot guess, where B comes from, when reading the code.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scope Variables and Generate Names 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!