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

5 visualizzazioni (ultimi 30 giorni)
tomoki fujii
tomoki fujii il 14 Set 2021
例えば
文字変数はA,B,Cの3種類あって
ユーザーがいずれかの文字をインプットし
Aなら2, Bなら4, Cなら7
を返すというように

Risposte (1)

Toshinobu Shintai
Toshinobu Shintai il 14 Set 2021
文字列系のブロック「String Constant」や「String Compare」を用いて、添付ファイルのようにすると作れるのではないでしょうか。添付モデルはR2021aで作成しています。
「my_func.slx」は文字列を比較して該当する数値を出力する機能、「system_model.slx」は参照モデルでmy_funcを呼び出しています。
  2 Commenti
tomoki fujii
tomoki fujii il 14 Set 2021
文字列変数をMATLABで配列を使って定義する方法はないでしょうか。
変数が数十個以上となると、ご提案の手法でもできなくはないですが規模が大きくなってしまいます。
Toshinobu Shintai
Toshinobu Shintai il 14 Set 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」にて定義しています。

Accedi per commentare.

Categorie

Scopri di più su プログラムによるモデル編集 in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!