MATLAB Functionブロックの使用方法

23 views (last 30 days)
O.E
O.E on 24 Nov 2017
Commented: O.E on 6 Dec 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の値です。

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on ループと条件付きステートメント in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!