MATLAB Functionブロックの使用方法

17 Ansichten (letzte 30 Tage)
O.E
O.E am 24 Nov. 2017
Kommentiert: O.E am 6 Dez. 2017
MATLAB Functionブロック内でSwitch文を用いてConstantの値を選択して、出力する機能を実装しビルドしたところ
以下のエラーメッセージが出力されビルドが中止されました。
変数 'code' は、いくつかの実行パス上で完全に定義されていません。
関数 'MATLAB Function' (#35.2845.2849)、行 126、列 4:
function y = fcn(u,ck,th)
switch ck
case 0
jug = u >= th(1,:);
jug = sum(jug);
switch jug
case 0
code = 0;
case 1
code = th(1,4);
case 2
code = th(1,3);
case 3
code = th(1,2);
case 4
code = th(1,1);
end
case 1
jug = u >= th(2,:);
jug = sum(jug);
switch jug
case 0
code = 0;
case 1
code = th(1,4);
case 2
code = th(1,3);
case 3
code = th(1,2);
case 4
code = th(1,1);
end
case 2
jug = u >= th(3,:);
jug = sum(jug);
switch jug
case 0
code = 0;
case 1
code = th(1,4);
case 2
code = th(1,3);
case 3
code = th(1,2);
case 4
code = th(1,1);
end
case 3
jug = u >= th(4,:);
jug = sum(jug);
switch jug
case 0
code = 0;
case 1
code = th(1,4);
case 2
code = th(1,3);
case 3
code = th(1,2);
case 4
code = th(1,1);
end
case 4
jug = u >= th(5,:);
jug = sum(jug);
switch jug
case 0
code = 0;
case 1
code = th(1,4);
case 2
code = th(1,3);
case 3
code = th(1,2);
case 4
code = th(1,1);
end
case 5
jug = u >= th(6,:);
jug = sum(jug);
switch jug
case 0
code = 0;
case 1
code = th(1,4);
case 2
code = th(1,3);
case 3
code = th(1,2);
case 4
code = th(1,1);
end
case 6
jug = u >= th(7,:);
jug = sum(jug);
switch jug
case 0
code = 0;
case 1
code = th(1,4);
case 2
code = th(1,3);
case 3
code = th(1,2);
case 4
code = th(1,1);
end
case 7
jug = u >= th(8,:);
jug = sum(jug);
switch jug
case 0
code = 0;
case 1
code = th(1,4);
case 2
code = th(1,3);
case 3
code = th(1,2);
case 4
code = th(1,1);
end
end
y =code;
変数ckはトリガ、thはConstantの値です。

Akzeptierte Antwort

Jiro Doke
Jiro Doke am 28 Nov. 2017
Bearbeitet: Jiro Doke am 29 Nov. 2017
全ての switch-case 構文に otherwise を追加してみてください。
つまり、例えば jug が0、1、2、3、4 以外 のケースにも対応していなければならないという事です。0、1、2、3、4しかありえないときは、 case 4 otherwise に変えれば良いと思います。
  1 Kommentar
O.E
O.E am 6 Dez. 2017
解決いたしました。
ありがとうございます。

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu ループと条件付きステートメント finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!