필터 지우기
필터 지우기

Assign a variable from within a function to the script I used to call the function from.

조회 수: 2 (최근 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

채택된 답변

Jan
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.
  댓글 수: 1
Christoffer Thornvall
Christoffer Thornvall 2015년 10월 4일
Thank you so much! I tried to read several articles about assignin but I did not understand them, nor the error messaged I received. But thank you again

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile 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!

Translated by