unassigned variable during loop execution of a function

3 vues (au cours des 30 derniers jours)
m h
m h le 16 Fév 2016
Commenté : m h le 16 Fév 2016
Hello everyone;
I have the following function: [sum_Power,Lambda,exitflag,sum_rate]=Global_MAX_EE(N0,nbr_R,h_j,h_ij,Sc,P_j,ep,r_j,ub,P_max);
I need to execute this function almost 200 or 300 times. However, as soon as I start the execution I get the following error: Output argument "sum_Power" (and maybe others) not assigned during call to "Global_MAX_EE".
The point is that this error accrue after 10 iterations and sometimes after 2, 3 iterations, it depends. In addition, each time I execute the function without any output argument everything goes well and I finish the 200 iterations. I checked all the functions I have and I am sure all the variables are well assigned and well used. Does any has any clue what to do?
I would be thankful for any suggestions or ideas.
Thank you in advance

Réponse acceptée

Star Strider
Star Strider le 16 Fév 2016
You most likely have one or more if blocks that assign those variables. In some instances, the if condition is not satisfied, and so the assignments of the variables within those blocks is not done. One way to avoid that error is to put this line as the first line in your ‘Global_MAX_EE’ function:
[sum_Power,Lambda,exitflag,sum_rate] = deal(NaN,NaN,NaN,NaN);
The NaN values will be overwritten as necessary in your later code.
The best approach is to find out the reason your if block assignments are not working correctly.
  3 commentaires
Image Analyst
Image Analyst le 16 Fév 2016
Did you ever find out why you declare it but never assign it elsewhere (other than in the deal() initialization)? I noticed that during the very brief time that you had the file attached. Since you don't use it, maybe you should just remove it from the output argument list.
Star Strider
Star Strider le 16 Fév 2016
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 16 Fév 2016
You declare that it will be an output but nowhere do you ever assign that variable. Find out why.
By the way, it's always a good idea to have the first lines in a function declare your output variables, even if they're null or zero or something. That way you'll avoid the error. In your calling code though you might have to check for valid values. For example, check if the variable is null and if it is then you know you never assigned it a proper values. So, for example, have the first lines in the function be this:
sum_Power = 0; % Whatever number indicates failure
Lambda = 0; % Whatever number indicates failure
exitflag = -1; % Whatever number indicates failure
sum_rate = []; % Null
  1 commentaire
m h
m h le 16 Fév 2016
thank you very much, indeed that what was wrong with my function.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by