![mlf.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238979/mlf.png)
MATLAB Function ブロックをフィードバックさせて演算をさせたい
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
SimulinkのMATALB Functionブロックを使って以下のようなことをしたいです。
入力:u = a (From Workspaceから読み込み)
出力:y = a + y[t-1]
もちろん、Delayブロックを用いれば簡単にできますが、MATLAB functionブロックを用いてコードを書くにはどのようにしたらよいでしょうか?
for文を使うのはわかるのですが、どのように書いたらいいのかわかりません。
初心者で稚拙な質問で大変申し訳ございません。
ご回答よろしくお願いいたします。
0 commentaires
Réponse acceptée
Shoumei
le 20 Sep 2019
MATLAB Functionブロックで前のサンプルのデータを使用したい場合はpersistent変数を使用します。persistent変数は、初期値を与える必要があるので、if isempty()で初期化を行います。
このように書くことができます。
function y = fcn(a)
persistent y_temp % 状態を持った変数
if isempty(y_temp)
y_temp = 0; % 初期化
end
y_temp = a + y_temp; % 前のサンプルのy_tempとaを加算して、y_tempに代入
y = y_temp;
このサンプルでは、Simulinkモデルは下図のように作成しています。
![mlf.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238979/mlf.png)
1 commentaire
Shoumei
le 20 Sep 2019
MATLAB Functionブロックの入力を、MATLABワークスペースから読み込みたいときは、プログラムのエディタ画面にある[データの管理] > 変数のスコープを[パラメータ]に設定します。
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur イベント関数 dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!