Problem with "if" command

7 ビュー (過去 30 日間)
Pietro Fiondella
Pietro Fiondella 2022 年 7 月 4 日
コメント済み: Torsten 2022 年 7 月 4 日
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?

採用された回答

Pooja Kumari
Pooja Kumari 2022 年 7 月 4 日
編集済み: Pooja Kumari 2022 年 7 月 4 日
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

その他の回答 (1 件)

Torsten
Torsten 2022 年 7 月 4 日
編集済み: Torsten 2022 年 7 月 4 日
Should be
if PBtime_nb>PBtime_b
instead of
if PBtime_b>PBtime_nb;
  2 件のコメント
DGM
DGM 2022 年 7 月 4 日
編集済み: DGM 2022 年 7 月 4 日
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 2022 年 7 月 4 日
The OP's code with two separate if-statements is not wrong ...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by