Simulinkで文​字変数を入力値として​読んで、それに対応す​る数値を返す方法を教​えて下さい。

2 views (last 30 days)
tomoki fujii
tomoki fujii on 14 Sep 2021
Commented: Toshinobu Shintai on 14 Sep 2021
例えば
文字変数はA,B,Cの3種類あって
ユーザーがいずれかの文字をインプットし
Aなら2, Bなら4, Cなら7
を返すというように

Answers (1)

Toshinobu Shintai
Toshinobu Shintai on 14 Sep 2021
文字列系のブロック「String Constant」や「String Compare」を用いて、添付ファイルのようにすると作れるのではないでしょうか。添付モデルはR2021aで作成しています。
「my_func.slx」は文字列を比較して該当する数値を出力する機能、「system_model.slx」は参照モデルでmy_funcを呼び出しています。
  2 Comments
tomoki fujii
tomoki fujii on 14 Sep 2021
文字列変数をMATLABで配列を使って定義する方法はないでしょうか。
変数が数十個以上となると、ご提案の手法でもできなくはないですが規模が大きくなってしまいます。
Toshinobu Shintai
Toshinobu Shintai on 14 Sep 2021
Simulinkでは文字列配列をサポートしていないため、変数が多数ある場合は効率よくモデリングを行うことができません。申し訳ありません。
代替案としては、列挙型を用いる方法があります。サンプルを添付しました。
「my_func_2.slx」では、MATLAB Functionブロックで入力された文字列と、列挙型を文字列に変換したものを比較しています。
function y = fcn(u, text_list)
y = text_list(1);
for i = 1:numel(text_list)
if strcmp(u, string(text_list(i)))
y = text_list(i);
end
end
end
「ENUM_TEXT」という列挙型、及び「text_list」という列挙型の変数は、「system_data.sldd」にて定義しています。

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!