unassigned variable during loop execution of a function
조회 수: 3 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Star Strider
2016년 2월 16일
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
Image Analyst
2016년 2월 16일
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.
추가 답변 (1개)
Image Analyst
2016년 2월 16일
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!