フィルターのクリア

Bug in Persistent variables? Simulink M-function

1 回表示 (過去 30 日間)
John
John 2018 年 1 月 30 日
編集済み: John 2022 年 9 月 8 日
I am unable to initialize a Persistent variable. Here's a snippet of code in a Simulink M-function which does not actually set 'a' no matter what I do:
persistent a
if isempty(a)
a = 1;
end
Either printing out 'a' or evaluating 'a' gives
a=[]
...NOT a=1 as it should.
It doesn't matter what the variable name is, or what I try to set it to, or how many variables there are. This bug happens with two different sets of Persistent Variables.
However, oddly, later on in the SAME M-function, I do another persistent declaration using the exact same method and there initialization DOES work.
I tried this in both R2016a and 2017b
What is going on?
  4 件のコメント
amin ya
amin ya 2020 年 1 月 29 日
I can confirm that this is a bug. I am on Matlab 2019b Update 3
John
John 2020 年 1 月 29 日
Amin, it's sad to see this major bug still exists after 2 years.
There has been no reply from Mathworks -- Mathworks, are there any plans to resolve this bug?

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

回答 (1 件)

John
John 2018 年 1 月 31 日
Okay, now I'm certain it's a BUG. The code WILL -- or will NOT -- properly initialize a variable depending on where it is located in the function, and what is around it. Even if the init is unrelated to the surrounding code.
Eg
<code 1>
persistent a
if isempty(a)
a = 1;
end
<code 2>
<code 3>
might NOT work, but
<code 1>
<code 2>
persistent a
if isempty(a)
a = 1;
end
<code 3>
WILL work.
The surrounding Code 1, 2, 3 are completely unrelated to the variable 'a'. Likewise, an init that DID work at a certain section will no longer work if it is moved.
Unfortunately, I cannot make the code work in places that I need it to, as it seems to be random why it works or is buggy.
Mathworks -- any response?
  2 件のコメント
Youssef Ismail
Youssef Ismail 2022 年 9 月 8 日
I also have the same problem has it been solved?
John
John 2022 年 9 月 8 日
編集済み: John 2022 年 9 月 8 日
Not resolved to my knowledge, unfortunately. You could try my workaround of finding a code order that may work for your code.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by