calculate how many state changes and time ThingSpeak

3 visualizzazioni (ultimi 30 giorni)
good morning!, I'm learning in MatlLab and ThingSpeak, and I'm a bit lost ... I have a variable that changes state from 0 to 1. And I want to know how I can calculate the times it changes state, and the time that remains for Example in state 1. Can you help me? Thank you so much

Risposta accettata

Simran
Simran il 28 Feb 2025
I see that you want to calculate the number of times your data’s state changes and the duration a variable remains in that specific state. To do so you can follow these steps:
1.) Retrieve your data from “ThingSpeak”.
% Define your channel ID and read API key
channelID = YOUR_CHANNEL_ID;
readAPIKey = 'YOUR_READ_API_KEY';
% Read data from ThingSpeak
data = thingSpeakRead(channelID, 'ReadKey', readAPIKey, 'Fields', 1, 'NumPoints', 8000);
2.) Once you have your data, you can calculate the number of times the variable changes state from 0 to 1 or 1 to 0 as follows.
% Calculate state changes
stateChanges = diff(data) ~= 0;
numStateChanges = sum(stateChanges);
3.) Now for calculating time spent by the variable in say state 1, you can find the indices where the variable is 1 and compute the time difference.
% Assume your data is sampled at regular intervals (e.g., every minute)
samplingInterval = 1; % in minutes
% Find indices where the state is 1
stateOneIndices = find(data == 1);
% Calculate the total time in state 1
totalTimeInStateOne = numel(stateOneIndices) * samplingInterval;
I did this with an example data, and the results were up and running.
You can refer to the following documentation:
thingspeak”-
thingSpeak Read” –
“find” function -

Più risposte (0)

Community

Più risposte nel  ThingSpeak Community

Categorie

Scopri di più su ThingSpeak in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by