Simple Problem with Pie Chart on Thingspeak using MATLAB Visualization

2 views (last 30 days)
I'm trying to create a pie chart with the energy comsumption of some of my electronic devices. I made a simple code for testing:
Flow the logic:
1) Receives energy consumption of a cicle (interval) from TV (field 1) and Lights (field 2) [ESP calculates in X seconds how much energy was used and sends it]
2) In 'Field Chart' I select 'Sum' as 'Daily', so each time it receives an energy consumption this value is added to the previous one.
Now about the MATLAB Visualization code:
I got a pie chart using 'pie(X)' with X being a vector of only 2 numbers (energy from TV and energy from Lights). But the sum of the only two numbers not equals 100%. One thing that I did notice is that the proportion of the yellow and the blue slices is correct (when compared with the values that are displayed at the Field Charts), but I don't know what is the missing part of the pie. Look at the pie chart that I got:
My objective is to show how one device affects the total daily consumption in comparison with another device. So, with the last value added to the daily sum of one field I want to use it as a slice of the pie in the chart. So, the objective is a Daily Pie Chart of Energy Consumtion.
I'm also getting the message: "Error using Testing (Pie Chart) (line 20). Channel ID must be a positive integer." But my code doesen't even have 20 lines and my Channel ID is a positive integer.
The simple code below:
readChannelID = 123456; % exemple of how the channelId written in the original code
fieldID1 = 1;
fieldID2 = 2;
readAPIKey = '6RW0V0UCJ7TPV24R';
etv = thingSpeakRead(readChannelID, 'Field', fieldID1, 'NumPoints', 1, 'Readkey', readAPIKey); % energy from TV
eil = thingSpeakRead(readChannelID, 'Field', fieldID2, 'NumPoints', 1, 'Readkey', readAPIKey); % energy from illumination
X = [etv eil];
pie(X);
Thanks for the help and sorry if i made an english mistake. I'm from Brasil.

Answers (1)

Vinod
Vinod on 4 Oct 2019
Does your data have NaN's in the field? You can find this out by exporting the channel data from the 'Data Import/Export' tab of the channel. If you do have NaN in the data, you will first want to remove the NaN's in the data before passing it to the PIE function.
Another note - you can optimize your code to make a single call to thingSpeakRead and read multiple fields from the channel. For example
ChannelData = thingSpeakRead(<CHANNELID>, 'Fields', [1 2], 'Numpoints',8000, 'Readkey', <READKEY>);
will give you 8000 points of field 1 and field 2 in a single call. You can access the first column using
ChannelData(:,1)
  3 Comments
Otávio Vanzelotti
Otávio Vanzelotti on 7 Oct 2019
For the problem with values < 1 I solved just by using an "if" :
  • if the sum of the numbers is <1 -> multiply each one by 100
But I still can't display in the pie chart the value of the daily sum (altough it's displayed as a "daily sum" in the field chart)
Vinod
Vinod on 8 Oct 2019
You can use the 'NumValues' optional argument to thingSpeakRead function with another optional parameter being 'outputFormat'. See documentation here.
Then from the resulting table you can sum up the values for the particular day and use that in your pie chart.

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Visualize Data in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!