Convert counter values to rate | MATLAB | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Convert counter values to rate
Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
1
November 3, 2016 - 12:33 am
sp_Permalink sp_Print

I've got the following code that de-cumulates counter values into a rate display using the matlab diff function (fired by a React every time data is inserted into the channel).  It works but seems like a kludge. Is there a neater way to do the same thing? I couldnt quite see how to do a multi-dimensional diff()

 

%read last 2 points

data = thingSpeakRead(readChannelID,'OutputFormat','Table','Numpoint',2, 'ReadKey', readAPIKey)

%get the latest point
d=data(2,:)
ts=d.Timestamps;
% difference each parameter
xdiff = diff(data.xcount)
if xdiff<0
    xdiff=d.xcount;
end    
ydiff = diff(data.ycount)
if ydiff<0
    ydiff=d.ycount;
end
zdiff = diff(data.zcount)
if zdiff<0
    zdiff=d.zcount;
end

%update new channel with the results.
thingSpeakWrite(writeChannelID,[xdiff,ydiff,zdiff],'WriteKey', writeAPIKey,'TimeStamp',ts);

Avatar
Adarsh_Murthy

Forum Posts: 59
sp_UserOfflineSmall Offline
2
November 4, 2016 - 8:24 am
sp_Permalink sp_Print

Hi, 

Could you share the "data" table? Without looking at the data, my suggestion would be to extract the numeric data from the "data" variable and then call diff() on the array. For example:

 

data = thingSpeakRead(readChannelID,'OutputFormat','Table','Numpoint',2, 'ReadKey', readAPIKey);

dataArr = data{:, 2:end};

diffArr = diff(dataArr);

 

diff() function supports 2D arrays. However, it doesn't support tables. The "Differences Between Matrix Rows" example on this page should be relevant to this case:

https://www.mathworks.com/help/matlab/ref/diff.html

In this case, "diffArr" variable should be 1xn row vector with the differences. Even with this, since you are looking to replace each variable in diffArr with a different value (if negative), the simple way is to use the if construct to check if it is negative and then replace the value.

 

HTH!  

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
3
November 4, 2016 - 8:13 pm
sp_Permalink sp_Print

Thanks Adarsh..   If I understand correctly there is no easier way to do the "if negative replace with value from another array" step.

Would it be easier if I used the "[data,timestamps]=thingSpeakRead(..)" form for the original extraction rather than a table? I'm a total newbie at Matlab and only used Table because I'm familiar with it.

Avatar
Adarsh_Murthy

Forum Posts: 59
sp_UserOfflineSmall Offline
4
November 8, 2016 - 10:23 am
sp_Permalink sp_Print

You could use arrays as you suggested (this would remove the additional line to extract data from the table):

data = thingSpeakRead(readChannelID,'Numpoint',2, 'ReadKey', readAPIKey);

diffArr = diff(data);

 

Regarding conditional replacement of values in an array, my suggestion would be to use if construct to keep the code readable. There are other techniques that you could consider, such as:

diffArr(1) = (diffArr(1) < 0)*d.xcount + (diffArr(1) > 0)*diffArr(1);
diffArr(2) = (diffArr(2) < 0)*d.ycount + (diffArr(2) > 0)*diffArr(2);
diffArr(3) = (diffArr(3) < 0)*d.zcount + (diffArr(3) > 0)*diffArr(3);

 

where each element of the diffArr is assigned a value depending on whether it is negative. For example, in the code snippet above, if the first element of diffArr is negative then it is replaced by d.xcount else it is replaced by its present value.

If you were replacing all negative elements in an array with the same value (instead of different values: d.xcount, d.ycount, d.zcount), there are other techniques that you could consider as well. 

Avatar
nmac

Silver
Forum Posts: 5
sp_UserOfflineSmall Offline
5
December 23, 2016 - 1:53 pm
sp_Permalink sp_Print

3 days to search .... but no result! 

Every minutes i've a counter value in a chanel.
How can i find a value (juste one !) one (year, day or hour) before ?

% For 'now' it's ok
Data1 = thingSpeakRead(202xxx,'Fields', 4, 'ReadKey','1F16B5PPNYxxxxxx');

% don't working for (year, day or hour) before
[Data2, time] = thingSpeakRead(202xxx,'Fields', 4, 'DateRange', [start, stop],'ReadKey','1F16B5PPNYxxxxxx');

Thank to you for help

Nicolas

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
6
December 25, 2016 - 2:31 am
sp_Permalink sp_Print

From the documentation:

Retrieve the data for August 8, 2014 through August 12, 2014 for fields 1 and 4 of a public channel, including the timestamp, and channel information.

[data,time,channelInfo] = thingSpeakRead(12397,'Fields',[1 4],'DateRange',[datetime('Aug 8, 2014'),datetime('Aug 12, 2014')]);

Did this not work for you?  Perhaps you are not formatting start and stop correctly- might be helpful if you submit more of your code showing how the start and stop times are derived.
  
Avatar
nmac

Silver
Forum Posts: 5
sp_UserOfflineSmall Offline
7
December 25, 2016 - 8:11 am
sp_Permalink sp_Print

I've see this documentation but it's not that i want, In that case, you have a specific date.

In my case I want to differentiate between a value from today and a value from (day or month before).

For that i've value of 'now' --> It's easy and ok

a = thingSpeakRead(202xxx,'Fields', 4, 'ReadKey','1F16B5PPNxxxxxxx');

a = 13376742

But i can't retrieve the data from there is a me from now

I try lots of things but nothing works; Each time it is to have full value

Date', datetime('start') ???

'DateRange', [t1,t2] ???

Avatar
nmac

Silver
Forum Posts: 5
sp_UserOfflineSmall Offline
8
December 25, 2016 - 10:25 am
sp_Permalink sp_Print

Correction about my translation:

--> But I can not retrieve a data from a month ago from now

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
9
December 25, 2016 - 11:47 pm
sp_Permalink sp_Print
Have you tried the datetime(year.month,day,hour,minute,second) format (see https://au.mathworks.com/help/matlab/ref/datetime.html)

This works for me using the public channel..

% Enter your MATLAB Code below
[data] = thingSpeakRead(12397,'Fields',[1 4],'DateRange',[datetime(2014,8,8,0,0,0),datetime(2014,8,8,1,0,0)],'OutputFormat','table')


data = 

  Timestamps  WindDirectionNorth0Degrees TemperatureF
 ____________________ __________________________ ____________

 08-Aug-2014 00:00:22 0 78.6 
 08-Aug-2014 00:01:22 270 78.6 
 08-Aug-2014 00:02:22 270 78.6 
 08-Aug-2014 00:03:22 225 78.6 
 08-Aug-2014 00:04:22 135 78.6 
 08-Aug-2014 00:05:22 315 78.7 
 08-Aug-2014 00:06:22 45 78.7 
Avatar
nmac

Silver
Forum Posts: 5
sp_UserOfflineSmall Offline
10
December 27, 2016 - 10:37 am
sp_Permalink sp_Print

Thank you for help.

I've find that i want :

% Time (month, day, hour ....) Here counter value in one hour
t1 = datetime('now') - hours(1);
t2 = datetime('now');
[b,time,channelInfo] = thingSpeakRead(202xxx,'Fields',[4],'DateRange',[datetime(t1),datetime(t2)], 'ReadKey','1F16B5PPxxxxxxx');
% Take first & end value
a = b (1);
c = b (end);

% Différence between end 1 first value
d = c-a;

Have a good year 2017 !

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
24 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

candacele1

WilliamBer

roxieob18

Metawatch

ozayagw

ubabeutiti

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee