MATLAB Functionブロックの使用方法

23 visualizzazioni (ultimi 30 giorni)
O.E
O.E il 24 Nov 2017
Commentato: O.E il 6 Dic 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の値です。

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su ループと条件付きステートメント in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!