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?

6 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Jan 2016
sum(diff(YourSignal) ~= 0)

More Answers (1)

Image Analyst
Image Analyst on 5 Jan 2016
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

Categories

Find more on Simulink in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!