Simulinkで文字変数を入力値として読んで、それに対応する数値を返す方法を教えて下さい。
5 views (last 30 days)
Show older comments
例えば
文字変数はA,B,Cの3種類あって
ユーザーがいずれかの文字をインプットし
Aなら2, Bなら4, Cなら7
を返すというように
0 Comments
Answers (1)
Toshinobu Shintai
on 14 Sep 2021
文字列系のブロック「String Constant」や「String Compare」を用いて、添付ファイルのようにすると作れるのではないでしょうか。添付モデルはR2021aで作成しています。
「my_func.slx」は文字列を比較して該当する数値を出力する機能、「system_model.slx」は参照モデルでmy_funcを呼び出しています。
2 Comments
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」にて定義しています。
See Also
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!