# Keep the latest desired value before changing to undesired value

4 ビュー (過去 30 日間)
Peppe 2023 年 7 月 17 日
コメント済み: Les Beckham 2023 年 7 月 19 日
Hello,
So let state that the signal X has four states [0,1,2,3]. The values 2,3 are not "good" values and you want to keep the value of the signal among the good ones as soon as the signal gets the bad value. Let me explain more:
at t = 0 X = 0 so nothing needs to happen
at t = 1 X = 1 so nothing needs to happen
at t = 2 X = 2 . This is the undesired value. we take the last desired value which was X=1
at t = 3 X = 3. This is undesired value. we take the last desired value which was X=1
at t = 4 X = 0. This is desired value. so X is now 0.
at t = 5 X = 1. This is desired value so X is now 1
So what I am trying to do is to save the last desired know value and I am trying to make it happen in Simulink. Not sure how to do this.

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

### 採用された回答

Peppe 2023 年 7 月 19 日
So what I did was the following:
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Les Beckham 2023 年 7 月 19 日
That looks like it should work. I was thinking about something like that. But, since I don't have Simulink to test with, I provided a different approach that I was able to test.

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

### その他の回答 (1 件)

Les Beckham 2023 年 7 月 18 日
There might be a way to do this using just basic Simulink blocks, but I don't have Simulink right now to test with. So, I'm going to suggest using a Matlab Function block with the following code in it:
function [out] = rejectUndesiredValues(in)
persistent lastout
undesiredValues = [2 3];
if ~exist('lastout', 'var')
lastout = 0; % default to outputting zero if an undesired value is the first input
end
if ~ismember(in, undesiredValues)
out = in; % update the output if the input is not an undesired value
else
out = lastout; % otherwise, repeat the last desired value
end
lastout = out; % update the past value of the output
end
% Let's test it
format compact
x = [0 1 2 3 0 1 0 3 2];
y = zeros(size(x));
for idx = 1:numel(x)
y(idx) = rejectUndesiredValues(x(idx));
end
disp(x)
0 1 2 3 0 1 0 3 2
disp(y)
0 1 1 1 0 1 0 0 0
function [out] = rejectUndesiredValues(in)
persistent lastout
undesiredValues = [2 3];
if ~exist('lastout', 'var')
lastout = 0; % default to outputting zero if an undesired value is the first input
end
if ~ismember(in, undesiredValues)
out = in; % update the output if the input is not an undesired value
else
out = lastout; % otherwise, repeat the last desired value
end
lastout = out; % update the past value of the output
end
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Peppe 2023 年 7 月 19 日
Thank. I made a solution as well. But please let me know if that is efficient.

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

### カテゴリ

Help Center および File ExchangeTest Model Components についてさらに検索

R2016b

### Community Treasure Hunt

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

Start Hunting!

Translated by