How can I make an object declared within a function persistent?

3 ビュー (過去 30 日間)
Warren Koontz
Warren Koontz 2015 年 6 月 16 日
コメント済み: Warren Koontz 2015 年 6 月 17 日
I have defined two handle classes for audio filters (LPcomb and APfilt). I would like to write a function to apply these filters to streaming audio on a block-by-block basis. So I declare the object variables to be persistent. In the "if isempty - end" block I try to instantiate these variables as objects of my classes. I get the following error message:
The following error occurred
converting from LPcomb to double:
Error using double
Conversion to double from LPcomb
is not possible.
So it seems that when you declare a variable to be persistent, it is declared as a double and cannot be instantiated as an object. How can I declare persistent objects in a function?
  1 件のコメント
Guillaume
Guillaume 2015 年 6 月 16 日
I cannot reproduce the problem. Can you post the code that is creating the error?

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

採用された回答

Eric Lin
Eric Lin 2015 年 6 月 17 日
As Guillaume notes, we'll need the code in question to help you further.
However, it is important to note that persistent variables are initialized as double arrays so a type change must first be performed if you would like the persistent variable to store non-double elements, as discussed in this answer: Trouble with persistent array of class objects
  1 件のコメント
Warren Koontz
Warren Koontz 2015 年 6 月 17 日
Thanks. The information in the link you provided fixed the problem. Here is a partial listing of the "before" code:
comb(Nc)=LPcomb;
for n=1:Ncf
comb(n)=LPcomb(cfDelay(n),damp,fbck);
end
Here is the same code "after"
comb=LPcomb;
for n=1:Ncf
comb(n)=LPcomb(cfDelay(n),damp,fbck);
end
In both cases, comb and Nc are persistent. No error message for the "after" version and the function worked fine. I didn't even get the warning about dynamic allocation!
Thanks for your help.
PS - This OO version ran MUCH more slowly than its non-OO predecessor!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCode Generation and Deployment についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by