Problem with "if" command

3 vues (au cours des 30 derniers jours)
Pietro Fiondella
Pietro Fiondella le 4 Juil 2022
Commenté : Torsten le 4 Juil 2022
I want to remane and reassigne some variable according 2 different cases
For example consider this script:
%In my script PBtime_b and PBtime_nb are obtained from other calculaion,
%(here i just assige a value) but im interested in a comparision between
%them in order to select other values
PBtime_b=5;
PBtime_nb=6;
Ab=5;
Cb=6;
Anb=7;
Cnb=8;
if PBtime_nb<=PBtime_b;
A=Ab
C=Cb
end
if PBtime_b>PBtime_nb;
A=Anb
C=Cnb
end
A
Unrecognized function or variable 'A'.
C
X=A+C
I would obtain A=Anb=5 and C=Cnb=6 but if i type A or C the script dosent recognize the variables
what i am doing wrong?

Réponse acceptée

Pooja Kumari
Pooja Kumari le 4 Juil 2022
Modifié(e) : Pooja Kumari le 4 Juil 2022
Hi Pierto
It is my understanding that you are facing "Unrecognized function or variable 'A'." error. And you want to rename and reassign some variable according to two different cases.
Please follow below updated code for the same in which you want to assign A=Anb= 5 and C= Cnb=6 as follows:
%In my script PBtime_b and PBtime_nb are obtained from other calculaion,
%(here i just assige a value) but im interested in a comparision between
%them in order to select other values
PBtime_b=5;
PBtime_nb=6;
Ab=5;
Cb=6;
Anb=7;
Cnb=8;
%you want A = Anb = 5 and C = Cnb = 6
if PBtime_nb>=PBtime_b;
A=Ab % Pbtime_nb = 6 > PBtime_b =5
C=Cb % A = 5, C = 6;
end
if PBtime_b>PBtime_nb;
Anb = A %Anb = 5
Cnb = C %Cnb = 6
end
A
C
X=A+C
For more infomation on variable Pre-allocation you can refer to the below link :
You can take help from workspace for checking correct variable assignment and you can refer to the link provided below:
Sincerely,
Pooja Kumari

Plus de réponses (1)

Torsten
Torsten le 4 Juil 2022
Modifié(e) : Torsten le 4 Juil 2022
Should be
if PBtime_nb>PBtime_b
instead of
if PBtime_b>PBtime_nb;
  2 commentaires
DGM
DGM le 4 Juil 2022
Modifié(e) : DGM le 4 Juil 2022
Since the cases appear to be mutually exclusive and non-independent, I don't see why the structure shouldn't be reduced to if-else.
if PBtime_nb<=PBtime_b;
A=Ab
C=Cb
else
A=Anb
C=Cnb
end
Torsten
Torsten le 4 Juil 2022
The OP's code with two separate if-statements is not wrong ...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Historical Contests dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by