Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Jan
Jan il 4 Ott 2015
Modificato: Jan il 4 Ott 2015
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 Commento
Christoffer Thornvall
Christoffer Thornvall il 4 Ott 2015
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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Scope Variables and Generate Names in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by