matlab and thingspeak manipulate data before plotting

2 views (last 30 days)
Hi, first of all I wish to specify I'm a complete noob with matlab. I'm using thingspeak to plot data send by a sensor. Basic thingspeak vialisation doesn't do what I wish so I'm looking for an easy solution to my problem without spending tons of hours to learn how this works. I already tried some stuffs but I just can't figure out how to modify to fit my needs. here my TS channe: https://thingspeak.com/channels/470434 1-the sensor print -299 value for an error in measurements, I wish to ignore theses points for the line created(so it doesn't bump down every time). But it could be interesting to be able to see the -299 value somewhere so I can figure out when the sensor did a wrong measurement. 2-Also I would be able to give -275 to all values between -275 and -298.
thanks in advance

Accepted Answer

Christopher Stapels
Christopher Stapels on 3 Oct 2018
Edited: Christopher Stapels on 5 Oct 2018
I would recommend setting up a new channel. Then you can set a react to call a MATLAB analysis to write the data to the new channel. First create the new MATLAB analysis. I would read the last data point from the channel.
f2Value=NaN;
value=str2num(webread('http://api.thingspeak.com/channels/470434/fields/1/last'));
if and((value>-299),(value<-274))
value=-275
end
if (value==-299)
value=NaN;
f2Value=1;
end
%now write to the new channel
thingSpeakWrite(<newID>,'Fields',[1,2],'Values',[value,f2Value],'WriteKey','XXXXXXXXXXXXXXXX');
Be sure to change the channel ID and the write API key.
Now create a new react and set the react to act on data entry for your first field, and make the criteria such that it will always be called (I like to use if field 1 value is not equal to 'fish'). Then select MATLAB Analysis as the action and choose the name of the MATLAB analysis you created.
Field 2 will show the number of times you got bad data, and field 1 will have the values you wanted.
  2 Comments
Simon Béland
Simon Béland on 4 Oct 2018
Ok thanks for your reply. I tried this but get an error. just some questions about this: 1- on which channel should I create the analysis? 2-I get an error on this: 21th line= thingSpeakWrite(594ThisIsMyNewChannel393,'Fields',[1,2],'Values',[value,f2Value],'WriteKey','J7xxxxxxxxx9');
Error using writeonanotherchannelfiltered (line 21) Error using thingSpeakWrite 'Values' must be a scalar, a 1-by-n numeric vector, or a cell array and must have the same dimensions as the 'Fields' value.
3-on which channel do I create the react?
thanks
Christopher Stapels
Christopher Stapels on 5 Oct 2018
1. MATLAB Analyses can be created independent of the channel. I really recommend you go through the getting started tutorial, just to get a handle on things: https://www.mathworks.com/help/thingspeak/getting-started-with-thingspeak.html Its pretty quick.
2. I edited the post above. Forgot to convert the string read from ThingSpeak to a number. value=str2num(webread('http://api.thingspeak.com/channels/470434/fields/1/last'));
3. I would set up the react on the channel that contains the data that you want to react to, probably the raw data channel.

Sign in to comment.

More Answers (0)

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on ThingSpeak in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!