How do I set counter = to a value when it is a persistent variable so if i reinitialize it each time through a loop it loses it purpose?

2 ビュー (過去 30 日間)
DJ V
DJ V 2016 年 11 月 28 日
コメント済み: DJ V 2016 年 11 月 28 日
function result = exp_average(a,varargin)
persistent avg counter;
if counter ==0
counter =0;
end
if nargin ==1
in1 = a
no = 1
end
if nargin ==2
in1 = a
in2 = b
no = 2
end
if no ==1 && counter ==0
b = 0.1;
a = in1;
avg = b * a+ (1-b)*avg;
counter = counter +1'
end
if no == 2 && counter ==0
clear exp_average;
a = in1;
b = in2;
avg = b*a+(1-b)*avg;
counter = counter+1;
end
if no ==1 && counter> 0
a = in1;
b = 0.1;
avg = b*a+(1-b)*avg;
counter= counter+1;
end
if no ==2 && counter>0
clear exp_average;
a = in1;
b = in2;
avg = b*a+(1-b)*avg;
counter = counter+1;
end
end
How do I set counter = to a value when it is a persistent variable so if i reinitialize it each time thruugh a loop it loses it purpose? I need to feed "or" and "and" statement logical operators with who numbered values. I'm no so long as counter isn't initialized.

回答 (1 件)

Daniel kiracofe
Daniel kiracofe 2016 年 11 月 28 日
I'm not sure if I understand the question. Here is my guess at what you are asking. If this isn't it, please restate the question.
Take a look at
https://www.mathworks.com/help/matlab/ref/persistent.html
The key line is " the persistent variable does not exist the first time you issue the persistent statement, it is initialized to the empty matrix."
So instead of
if counter ==0
counter =0;
end
Try this instead
if (isempty(counter))
counter =0;
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by