How can I get rid of the error "The value assigned to variable 'n' may be unused"?

120 次查看(过去 30 天)
Hi, I am trying to write a code to alternatively charge and discharge a battery in simulink. I want the battery to discharge from 80% down to 40% and then the switches in the circuit to close (or open) and recharge the battery until it is 80% again and keep doing this. As this means that between 40% and 80% the battery could be either charging or discharging depending on what percentage value it started from, I defined a further variable 'n' which I am trying to update each time a measurement is taken. I've written an if/else loop to try to implement the charge control but I keep getting an error stating that "The value assigned to variable 'n' may be unused". Please can someone explain how I can get rid of this error - I don't want to just get rid of the statements because I want to use the new value of n to update the if conditions but I can't see where I am going wrong with it!? Code is provided below. Thanks in advance!
n = 1;
if n == 1
C_On = 0;
D_On = 1;
if (a >= 80 || (a < 80 && a > 40))
n = 1;
else
n = 0;
end
elseif n == 0
C_On = 1;
D_On = 0;
if (a <= 40 || (a > 40 && a < 80))
n = 0;
else
n = 1;
end
else
C_On = 0;
D_On = 0;
end

采纳的回答

DGM
DGM 2021-3-16
编辑:DGM 2021-3-16
The note about "variable may be unused" is a warning, not an error. I'm not sure how this applies to your code example and description, as 'n' is a constant and 'a' is undefined. Both n and a appear to be used by tests, though I'm assuming that this is only the interior of a loop, so I really can't know what's missing.
The 'may be unused' warning usually pops up when a value is assigned to a variable and then it is immediately overwritten before the new value is used. If the loop indeed starts with n being assigned a constant value, then its reassignment within the conditional structure is of no consequence. Any changes to n will be overwritten as soon as the next cycle starts. Perhaps the initialization of n=1; is supposed to be outside of the loop?
If it's a warning, it shouldn't prevent the code from running. If your code is doing what you want and the warning is something you find bothersome, you can disable it in preferences. Under Preferences>MATLAB>CodeAnalyzer, search for the error string and disable the relevant one.

更多回答(1 个)

José M. Requena Plens
In the lines where this warning appears, add the following comment:
%#ok<NASGU>
I have not gotten the warning with your code. Anyway, it is not necessary to assign again the same value that it has when entering the condition, the simplified code would be:
n = 1;
if n == 1
C_On = 0;
D_On = 1;
if ~(a >= 80 || (a < 80 && a > 40)) % Change n value
n = 0;
end
elseif n == 0
C_On = 1;
D_On = 0;
if ~(a <= 40 || (a > 40 && a < 80)) % Change n value
n = 1;
end
else
C_On = 0;
D_On = 0;
end
  1 个评论
CharlotteHF96
CharlotteHF96 2021-3-16
Thank you for your answer/help! I'm not very experienced with matlab so didn't know if I needed to redefine the value of n to get the loop to update.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by