how to detect vector value in sequence in stateflow

조회 수: 4 (최근 30일)
Luhur
Luhur 2011년 6월 9일
Hi
i have large vector data, let say vector(1x200) in stateflow that the value is between 1 to 10, i want to detect at least 10 consecutive value on that vector (1x200) is bigger than 5 then the action y= 1000, for example:
vector(1x10): X = [3 4 6 6 6 6 7 2 1 3]
in that vector, vector 3 to 7 has value: bigger than 5 and 5 value (3 to 7) in sequence ,, then the condition is accepted so the value y=1000
other example: X = [3 4 6 6 6 6 4 7 1 3]
in that vector, there are vector value that bigger than 5 BUT not 5 value in sequence, cause the vector 7 value is 4,only 4 in sequence, so the condition is not accepted...
vector input value is from simulink, the question is how was the script on stateflow?? or any suggestion???

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 6월 9일
Ahh! Nice clarification.
%Sample Data
X1=[1 1 6 6 6 7 7 2 3 4];% correct
X2=[1 1 6 6 6 7 4 2 3 4];% wrong
X3=[8 8 9 9 8 0 0 1 1 2];% condition: correct
X4=[8 8 9 9 2 8 0 1 1 2];% condition: wr
%Engine
correct = @(x)any(diff(find(diff([0 x]>5)))>=5); %is it correct?
%Checks
correct(X1)
correct(X2)
correct(X3)
correct(X4)
  댓글 수: 3
Sean de Wolski
Sean de Wolski 2011년 6월 9일
if correct(X1) %copied from Matt Fig
y = 1000;
else
%whatever
end
Fangjun Jiang
Fangjun Jiang 2011년 6월 9일
Thanks, Sean! I suspect it could be done in a way other than searching but I just couldn't come up with it. Smart!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 6월 9일
This task is not that straightforward even in Matlab. I can suggest one way to do it in Matlab. Please chime in if you have other ways.
X=ceil(10*rand(1,200));
A=X>5;
B=num2str(A);
C=strfind(B,repmat('1 ',1,5)); %5 is the number of consecutive occurance
if any(C)
Y=1000
end
With this, it can be done in Stateflow but it will require using the "ml namespace operator" (see reference "Using Matlab Functions and Data in Actions"). I am not sure if you could pass those obstacles. Maybe you should re-consider your approach and use the Embedded Matlab Function block.
  댓글 수: 6
Sean de Wolski
Sean de Wolski 2011년 6월 9일
Original poster.
Luhur
Luhur 2011년 6월 9일
to sean de..
it only a simple condition:
1. the value must in sequence in order the place of vector,for example the vector value must have value=4 and 5 times in a row (sequence)
X=[0 0 0 4 4 4 4 4 0 0 0] condition= correct
X=[4 4 4 4 4 0 0 1 1 2 2] condition= correct
X=[0 0 1 1 2 2 4 4 4 4 4] condition= correct
X=[4 0 4 4 4 4 3 2 2 1 1] condition= wrong
2. the value must bigger (>) 5, for example
if we combine two condition above then:
X=[1 1 6 6 6 7 7 2 3 4] condition: correct
X=[1 1 6 6 6 7 4 2 3 4] condition: wrong (because not 5 times in a row and the value of vector (1x7)=4 is bigger than 5)
X=[8 8 9 9 8 0 0 1 1 2] condition: correct
X=[8 8 9 9 2 8 0 1 1 2] condition: wrong (because only 4 times in a row the value is bigger than 5, the vector (1x5)= 2 then its wrong)
to Fangjun, i will try first... thanks...

댓글을 달려면 로그인하십시오.


Matt Fig
Matt Fig 2011년 6월 9일
Another alternative:
correct = @(x) any(strfind(x>5,[1 1 1 1 1]))
if correct(X1)
y = 1000;
else
% Whatever
end
  댓글 수: 4
Matt Fig
Matt Fig 2011년 6월 9일
This use for FINDSTR and STRFIND is one of those hidden gems of MATLAB! It is very useful and fast...
Luhur
Luhur 2011년 6월 9일
THANK YOU SO MUCH SEAN DE, MATT FIG and FANGJUN JIANG... :)
don't know it helps other knowledge... thanks again for fast respond!!
wait my other question... :P

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Stateflow에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by