Adding Multiple Fields Into One Field to Plot Graph | MATLAB IoT Analytics | Forum

The ThingSpeak community site has been upgraded to a new site. This site is currently in read-only mode. You can ask questions or post and read discussions on the new site.

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
Adding Multiple Fields Into One Field to Plot Graph
No permission to create posts
April 16, 2019
4:59 pm
New Member
Forum Posts: 1
Member Since:
April 16, 2019
sp_UserOfflineSmall Offline

I am measuring the flow-rate of water via LoraWAN and have been able to send a string in Hex to Thingspeak. I have been able to decode the string which has three flow-rates in it at 10,20 and 30 liters of water through meter. Each flow rate is in a different field (Necessary for decoding). Now I want to plot a single graph where I can plot all three fields one after the other on the same Y-axis. Is it possible? The MATLAB code on Thingspeak is as follows:

% Template MATLAB code for reading data from a public channel, analyzing
% the data and storing the analyzed data in another channel.
% Prior to running this MATLAB code template, assign the channel ID
% to read data from to the 'readChannelID' variable.
% To store the analyzed data, you will need to write it to a channel other
% than the one you are reading data from. Assign this channel ID to the
% 'writeChannelID' variable. Also assign the write API Key to the
% 'writeAPIKey' variable below. You can find the write API Key in the right
% side pane of this page as well.
% TODO - Replace the [] with channel ID to read data from:
readChannelID = 759188;
readChannel2ID = 760065;
% TODO - Replace the [] with channel ID to write data to:
writeChannelID = 760065;
% TODO - Enter the Write API Key between the '' below:
writeAPIKey = 'AO70U2FZSFK1RFBP';
%% Read Data %%
%[data, time] = thingSpeakRead(readChannelID);
go = 1;
%while go

data = thingSpeakRead(readChannelID,'OutputFormat','table');
%% Analyze Data %%
% Add code in this section to analyze data and store the result in the
%data1 = '2054202033302E333736202054202020482034392E353732204820204620323432352E3020462020433432384320'
data_str=char(transpose(sscanf(char(data1), '%2x')))
%a = extractAfter(data_str,"T")
% String parts of overall converted data
P = extractBetween(data_str,"!","@");
F1 = extractBetween(data_str,"@","$");
F2 = extractBetween(data_str,"$","%");
F3 = extractBetween(data_str,"%","^");
% string to double conversion
Pulse = str2double(P);
Flow1 = str2double(F1);
Flow2 = str2double(F2);
Flow3 = str2double(F3);
% assigning data to channel fields
data.TotalPulses = Pulse;
data.FlowOne = Flow1;
data.FlowTwo = Flow2;
data.FlowThree = Flow3;
%% Write Data %%
thingSpeakWrite(writeChannelID, data.TotalPulses, 'WriteKey',k, 'Fields' , 1);
pause (15);
thingSpeakWrite(writeChannelID, data.FlowOne, 'WriteKey',k, 'Fields' , 2);
pause (15);
thingSpeakWrite(writeChannelID, data.FlowTwo, 'WriteKey',k, 'Fields' , 2);
thingSpeakWrite(writeChannelID, data.FlowThree, 'WriteKey',k, 'Fields' , 2);
pause (15);
%thingSpeakWrite(writeChannelID, data.FlowOne , 'Fields', 4, 'WriteKey', writeAPIKey);
%thingSpeakWrite(writeChannelID, [data.TotalPulses,data.FlowOne,data.FlowTwo,data.FlowThree], 'WriteKey',k, 'Fields' , [1,2,3,4]);

April 17, 2019
2:41 pm

Forum Posts: 885
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

I would recommend that you organize your data so each field is used for a different measurement, as in the last command you have commented out.

%thingSpeakWrite(writeChannelID, [data.TotalPulses,data.FlowOne,data.FlowTwo,data.FlowThree], 'WriteKey',k, 'Fields' , [1,2,3,4]);

The total MATLAB compute time for a free account is less than 20 seconds, so you will not be able to use the pause commands if you have a free account.
When you write the fields separately, you have the update rate problem and you also write null values in the fields that are not updated, which makes the data harder to plot and parse.

Your original question was how to plot a single graph. I would suggest using MATLAB visualizations. Here is a good relevant example:

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
32 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

kusmumichael, petersmith99, Rambant, blakeharriss09, optisol, Niyonzima Fils

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457