I have a signal of a switch with ON/OFF situation. How can I get how many times it actuates by matlab, simulink, or script?

5 次查看(过去 30 天)
When the value is not equal to previous value, the counter should be increased by 1. I want total count in a certain time period.

采纳的回答

Walter Roberson
Walter Roberson 2016-1-5
sum(diff(YourSignal) ~= 0)

更多回答(1 个)

Image Analyst
Image Analyst 2016-1-5
Maybe something like this:
loopCounter = 0;
count = 0;
oldSwitchReading = -1;
stillCheckingSwitch = true;
while stillCheckingSwitch && loopCounter < 1000000
thisSwitchReading = % Some code to read your switch.....
if thisSwitchReading ~= oldSwitchReading
count = count + 1;
end
oldSwitchReading = thisSwitchReading; % New/current state is now the old state for the next iteration.
loopCounter = loopCounter + 1; % Increment fail-safe.
% Decide if we need to quit checking the switch
stillCheckingSwitch = % Code to determine when to quit.....
end

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by