Problem with "if" command

7 views (last 30 days)
Pietro Fiondella
Pietro Fiondella on 4 Jul 2022
Commented: Torsten on 4 Jul 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?

Accepted Answer

Pooja Kumari
Pooja Kumari on 4 Jul 2022
Edited: Pooja Kumari on 4 Jul 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

More Answers (1)

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

Sign in to comment.

Categories

Find more on Historical Contests in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!