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

122 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

DGM
DGM il 16 Mar 2021
Modificato: DGM il 16 Mar 2021
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.

Più risposte (1)

José M. Requena Plens
José M. Requena Plens il 16 Mar 2021
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 Commento
CharlotteHF96
CharlotteHF96 il 16 Mar 2021
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.

Accedi per commentare.

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by