MATLAB function block initialization problem.

7 ビュー (過去 30 日間)
Sandip Bhamare
Sandip Bhamare 2021 年 5 月 20 日
コメント済み: Jonas 2021 年 5 月 20 日
I am using the matlab function block to execute the logic. For that i am using some variables but they get initialize at every sample time. Due to this my logic is not working properly. I want the variable only get initialize onces in whole simulation. so any possible solution to this type of problem?

回答 (1 件)

Jonas
Jonas 2021 年 5 月 20 日
編集済み: Jonas 2021 年 5 月 20 日
Define your variable as being persistent, to remember its value across executions.
Also, add logic that writes an initialisation value to it only on first execution, as follows:
persistent variable_you_want_to_remember
if isempty(variable_you_want_to_remember)
variable_you_want_to_remember = 12; % initialisation
end
When using a persistent variable, the value will be '[]' on first execution, that's why we can use the function isempty to see if we are on first execution or not.
The next executions, the value of the value will be remembered from last execution.
  2 件のコメント
Sandip Bhamare
Sandip Bhamare 2021 年 5 月 20 日
thank you Jonas for the reply. I had gone through same procedure in my code for the variable defination and initialization, but the code is not executed as per expectations. some persistent variabls are get intialized after some execution. how to overcome that problem?
Jonas
Jonas 2021 年 5 月 20 日
Please share your code or a minimal example that demonstrates your issue.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeModeling についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by