How to save variables generated inside 'for' loop which contains a function, to the workspace?

I have a long code, part of which involves calling a function inside a 'for' loop. Please see code below:
zz = linspace(0,0.2,length);
for qq=1:1:length(zz)
% some stuff %
[Z, AT, AW, W] = myfunc(zz(qq));
A = AT(end,:);
end
'myfunc' is written as a separate .m file. The problem is the variable 'A' or any of the variables returned from the function is not available outside this loop. What can I do save these variables so that I can use them in the next steps of my code?

10 Kommentare

Hi,
Do you mind to share your script? So that we have better picture explain to you.
Variable 'A' should be available in the workspace except your for-loop is inside a function.
The script is more than 200 lines of code. It will be difficult to share it here.
The problem is occurring only when I am using the loop. If there is no loop, I can use the variables returned by the function.
"The script is more than 200 lines of code. It will be difficult to share it here."
Actually it is really easy: just click the paperclip button to upload it as a file.
Thanks, Stephen. However I am not sure if I am allowed to share the whole script here as it is part of an ongoing project. Is it really not possible to identify the problem without seeing the whole script?
Like I said, the problem is only occurring when it is inside the for loop. Otherwise, I can use the variables returned.
A = AT(end,:);
should be
A(qq,:) = AT(end,:);
Otherwise you are overwriting all of A each time through the qq loop.
A is a row vector, and I only want AT(end,:) to be stored in A. So I think this line is correct?
Even if I leave out the part of storing in 'A', I can't use any of the variables outside the for loop. They are not stored in the workspace. But without any loop the variables are stored.
Where is the A result for qq = 1 to be stored separately for the A result for qq = 2 ? If you only want the final A, the one for qq = length(zz) then why bother to run any iteration other than qq = length(zz) ?
Perhaps you would find it easier to understand as
A{qq} = AT(end,:);
Is it possible that you are using parfor? With you not indexing the output variables by qq, parfor would assume that those variables are local variables that do not need to be preserved after the parfor loop.
Walter, perhaps this should clear things a bit.
A = sqrt(P_0)*exp((-(1+1i*chirp)/2)*(T/t_0).^2);
for qq=2:1:length(zz)
Pavg = sum((abs(A)).^2)/Tr*delta_t;
G = g0/(1+ Pavg/PumpSat);
L = G/2 + (G/2)^2;
[Z, AT, AW, W] = myfunc(T, A, w0, gamma, fr, RT, L, ...
zz(qq), nsaves);
A = AT(end,:);
end
Outside the loop I need to use AT and AW, which I am not being able to. Also, I am not using parfor.
Your loop starts at 2. What happens if length(zz) is 1 ?
Be careful with length(), as it will report 0 if any dimension is empty, and will return the largest dimension otherwise, not a particular dimension.
In my original question:
zz=linspace(0,0.2,Length);
I am starting from qq=2 as qq=1 has value 0, and that is not needed in my calculation.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

u can create an array, and use the function vertcat to store the variable A each time

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2016a

Gefragt:

am 2 Sep. 2018

Erneut geöffnet:

am 22 Dez. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by