MATLAB Functionブロックの使用方法

17 次查看(过去 30 天)
O.E
O.E 2017-11-24
评论: O.E 2017-12-6
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の値です。

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 ループと条件付きステートメント 的更多信息

Community Treasure Hunt

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

Start Hunting!