how to detect vector value in sequence in stateflow
显示 更早的评论
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???
采纳的回答
更多回答(2 个)
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
2011-6-9
I would use diff a few times to find where there are changes and to ensure they're either zero or one. I didn't totally understand the OP's question; however.
Fangjun Jiang
2011-6-9
What do you mean, Sean? I am not following your comments.
Sean de Wolski
2011-6-9
consecutive values in a vector (either the same or with an increment of one) will have a value of 0 or 1 when you take the diff of it.
>> diff([0 3 2 4 4 4 4 5 6 9 2])
I think the OP has refined the question and this approach may be wrong. Like I said I don't really understand the question, so nevermind I guess ;-|
Fangjun Jiang
2011-6-9
What does OP stand for?
Sean de Wolski
2011-6-9
Original poster.
Luhur
2011-6-9
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 个评论
Sean de Wolski
2011-6-9
That takes about 80% of the time mine does.
Fangjun Jiang
2011-6-9
Thanks, Matt! I didn't know that you can use strfind() directly on data arrays.
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
2011-6-9
类别
在 帮助中心 和 File Exchange 中查找有关 Stateflow 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!