If loop not executed in a function

1 回表示 (過去 30 日間)
Shuo Zhang
Shuo Zhang 2019 年 7 月 12 日
コメント済み: KSSV 2019 年 7 月 12 日
Hi everyone,
I'm trying to build a simple function that gives values to parameters based on input. Attached are my programs
In the main program, if I input ship_type='Passenger ship', then everything worked fine
But when I change it to ship_type='cargo ship' or 'container ship', I get the error message 'Matrix dimensions must agree'
So I'm confused.
What's wrong with my program?
Btw, I'm using version 2017a

採用された回答

KSSV
KSSV 2019 年 7 月 12 日
編集済み: KSSV 2019 年 7 月 12 日
Use strcmp instead of equating.
function[A0,A1,A2,A3,A4,A5]=Trial_types(ship_type,x)
if strcmp(ship_type,'Passenger ship')
A0=0;
A1=1+x;
A2=2*x;
A3=3/x;
A4=4-x;
A5=x^2;
elseif strcmp(ship_type,'cargo ship')
A0=10;
A1=10+x;
A2=20*x;
A3=30/x;
A4=40-x;
A5=(x-10)^2;
elseif strcmp(ship_type,'container ship')
A0=20;
A1=20+x;
A2=30*x;
A3=40/x;
A4=50-x;
A5=(x-20)^2;
else
end
end
  2 件のコメント
Shuo Zhang
Shuo Zhang 2019 年 7 月 12 日
Thank you very much for your help! It worked perfect!
KSSV
KSSV 2019 年 7 月 12 日
You can also use switch. Read about it.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by