Problem with "if" command

3 visualizaciones (últimos 30 días)
Pietro Fiondella
Pietro Fiondella el 4 de Jul. de 2022
Comentada: Torsten el 4 de Jul. de 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?

Respuesta aceptada

Pooja Kumari
Pooja Kumari el 4 de Jul. de 2022
Editada: Pooja Kumari el 4 de Jul. de 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

Más respuestas (1)

Torsten
Torsten el 4 de Jul. de 2022
Editada: Torsten el 4 de Jul. de 2022
Should be
if PBtime_nb>PBtime_b
instead of
if PBtime_b>PBtime_nb;
  2 comentarios
DGM
DGM el 4 de Jul. de 2022
Editada: DGM el 4 de Jul. de 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 el 4 de Jul. de 2022
The OP's code with two separate if-statements is not wrong ...

Iniciar sesión para comentar.

Categorías

Más información sobre Historical Contests en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by