Convert counter values to rate | MATLAB | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





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

sp_Feed sp_TopicIcon
Convert counter values to rate
No permission to create posts
November 3, 2016
12:33 am
Avatar
rw950431

Top
Members
Forum Posts: 262
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

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);

November 4, 2016
8:24 am
Avatar
Adarsh_Murthy

MathWorks
Members
Forum Posts: 62
Member Since:
August 25, 2015
sp_UserOfflineSmall Offline

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!  

November 4, 2016
8:13 pm
Avatar
rw950431

Top
Members
Forum Posts: 262
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

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.

November 8, 2016
10:23 am
Avatar
Adarsh_Murthy

MathWorks
Members
Forum Posts: 62
Member Since:
August 25, 2015
sp_UserOfflineSmall Offline

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. 

December 23, 2016
1:53 pm
Avatar
nmac

Silver
Members
Forum Posts: 5
Member Since:
January 5, 2015
sp_UserOfflineSmall Offline

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

December 25, 2016
2:31 am
Avatar
rw950431

Top
Members
Forum Posts: 262
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

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.
  
December 25, 2016
8:11 am
Avatar
nmac

Silver
Members
Forum Posts: 5
Member Since:
January 5, 2015
sp_UserOfflineSmall Offline

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] ???

December 25, 2016
10:25 am
Avatar
nmac

Silver
Members
Forum Posts: 5
Member Since:
January 5, 2015
sp_UserOfflineSmall Offline

Correction about my translation:

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

December 25, 2016
11:47 pm
Avatar
rw950431

Top
Members
Forum Posts: 262
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline
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 
December 27, 2016
10:37 am
Avatar
nmac

Silver
Members
Forum Posts: 5
Member Since:
January 5, 2015
sp_UserOfflineSmall Offline

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:
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

BarryMaync, ThomasRougs, DonaldGen, prince, JrGordon, Elamsweems

Administrators: Hans: 387, lee: 457