Azzera filtri
Azzera filtri

Addressing arrays between functions

3 visualizzazioni (ultimi 30 giorni)
Andy McCann
Andy McCann il 6 Mag 2012
I'm running a while loop and i need to use a value generated within the loop to provide a different variable from a different script and i'm having trouble getting the generated variable into the other functions. Here's what i have, if anyone has any ideas it'd be greatly appreciated.
function R_New = R_New (theta, phi, Params, LaserParams, Orientation)
run_number = 1;
Conv{1} = 0;
while Conv{run_number} == 0
R_initial = Params(1);
R_1{run_number} = R_dash(theta, phi, Params, LaserParams, run_number,Orientation);
Radius = R_Sphere(theta, phi, Params, LaserParams, run_number, Orientation);
R_2{run_number} = R_1{run_number}.*(R_initial/Radius);
Conv{run_number} = Convergence(theta, phi, Params, LaserParams, run_number, Orientation);
run_number = run_number +1;
end
Where R_2 is the generated variable, used in the 2nd iteration of the loop in function R_dash.
Here is R_dash
function R_dash = R_dash(theta, phi, Params, LaserParams, run_number, Orientation, R_2)
I just dont know how to get R_2 into the R_dash function so that i can access the precious values of it.
Thanks
if run_number == 1;
R = R_0(Params);
else
R = R_2{run_number};
end
Pert = P(theta, phi, Params, LaserParams, run_number, Orientation);
R_dash = R + Pert;
  1 Commento
Jan
Jan il 6 Mag 2012
Are you working with Dougie, who has asked a very similar question? Or do you use multiple accounts? See: http://www.mathworks.de/matlabcentral/answers/37341-for-loop
Please format your code and explain, what the code fragment after "Thanks" means.

Accedi per commentare.

Risposta accettata

Jan
Jan il 6 Mag 2012
The question is not clear. If you want to access a variable inside a function, you have to insert it in the input arguments. In your case the function definition is:
function Output = R_dash(theta, phi, Params, LaserParams, run_number, Orientation, R_2)
I'd avoid to use the same name for the function and its output. This is at least confusing. Anyhow, here you have defined R_2 as last parameter. But you call this function by this:
R_1{run_number} = R_dash(theta, phi, Params, LaserParams, run_number,Orientation);
without the last argument. So perhaps you want to insert it here? But of course you need a value for it in the first iteration - this could be the orphaned code fragment after the "Thanks" in your question?
  2 Commenti
Andy McCann
Andy McCann il 6 Mag 2012
Yeah sorry, it's just different accounts from different computers.
I've changed the function to call R_2 and given it an an initial value, however now i'm getting an error of
??? Index exceeds matrix dimensions.
Error in ==> R_dash at 30
R = R_2{run_number};
Error in ==> R_Check at 20
R_1{run_number} = R_dash(theta, phi, Params, LaserParams, run_number,Orientation,
R_2);
Here's the tidied code for R_New
function R_New = R_New (theta, phi, Params, LaserParams, Orientation)
run_number = 1;
Conv{1} = 0;
while Conv{run_number} == 0
R_2{1} = R_0(Params);
R_initial = Params(1);
R_1{run_number} = R_dash(theta, phi, Params, LaserParams, run_number,Orientation, R_2);
Radius = R_Sphere(theta, phi, Params, LaserParams, run_number, Orientation);
R_2{run_number} = R_1{run_number}.*(R_initial/Radius);
Conv{run_number} = Convergence(theta, phi, Params, LaserParams, run_number, Orientation);
run_number = run_number +1;
end
end
And here os R_dash
function R_dash = R_dash(theta, phi, Params, LaserParams, run_number, Orientation, R_2)
R = R_2{run_number};
Pert = P(theta, phi, Params, LaserParams, run_number, Orientation);
R_dash = R + Pert
Jan
Jan il 7 Mag 2012
The error message is clear: You want to access R_2{run_number}, but R_2 has less then run_number elements.
As far as I understand the guidelines, it is not allowed to have multiple accounts: http://www.mathworks.com/accesslogin/faq.jsp?s_cid=mwa-cmlndefq
The former question has been a duplicate post already, which was merged by Walter. No you are continuing to ask for the same problem without a link to the former thread and under a different user name.
Please do not post multiple questions for one problem and do not use multiple accounts, because it confused and impedes the contributors, who want to assist you.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by