How to plot data from JSON Structure array

조회 수: 16 (최근 30일)
Dharmesh Joshi
Dharmesh Joshi 2022년 5월 9일
댓글: Jon 2022년 5월 16일
Hi
I have used HTTP to download a large amount structued data from an API.
This is my code
function n02()
downloaded_data = device_data("864475047548549","2022-04-08T10:31:00Z","2022-05-08T11:31:00Z");
%struct2table(downloaded_data)
plot(downloaded_data.Timestamp,downloaded_data.iot_battery);
end
This is the output i am getting
Error using plot
Invalid first data argument.
Error in n02 (line 6)
plot(x,y);
What am i doing wrong

채택된 답변

Jon
Jon 2022년 5월 9일
편집: Jon 2022년 5월 9일
I don't understand what you are expecting to happen in your example code. Your variable downloaded_data is assigned to an array of 3 strings. The variable downloaded_data doesn't have any fields and in particular not the ones you are trying to plot.(Timetstamp or iot_battery).
Here's a small example of how you should be working with the json data and plotting it. Hopefully you can follow from this and adapt your code accordingly
If you have some json text, txt, you can turn it into a structure using jsondecode(txt)
So for example
txt = '{"t":[1, 2, 3, 4, 5],"x":[1, 4, 9, 16, 25]}'
mydat = jsondecode(txt)
plot(mydat.t,mydat.x)
  댓글 수: 5
Dharmesh Joshi
Dharmesh Joshi 2022년 5월 16일
Ok, done, sorry for the time duration was trying to see how to do this. Then noticed that i had to undo the previous answer.
Jon
Jon 2022년 5월 16일
Thanks, good luck with your project

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Dharmesh Joshi
Dharmesh Joshi 2022년 5월 9일
Hi Jon
Yes, if i call the plot as
plot([downloaded_data.iot_battery])
it works. It seems that i had to enter [ ]. Is there a reason for this?
My data also includes a timestamp, which i belive it in a text formate as
'2022-04-08T10:41:33.3189578+00:00'
How do get this information on my x axis?
  댓글 수: 12
Dharmesh Joshi
Dharmesh Joshi 2022년 5월 11일
Thanks I got it working
One issue i noticed on the plots is that there is some blank spots. I presume at that plot is assume certain time stamp and if there is no data for that timestamp it showing blank space. Please see the attached image.
Jon
Jon 2022년 5월 11일
The blank spots probably correspond to points where the temperature value is nan (not a number), so as you say missing data.

댓글을 달려면 로그인하십시오.


Dharmesh Joshi
Dharmesh Joshi 2022년 5월 11일
Is there away to confirm if there is a nan, as my device are updating every 1 minute, if there is any issue , there would be not entry for that minute or a perticular field would be 0.
I noticed the following warning
Warning: Error updating Text.
String scalar or character vector must have valid interpreter syntax:
Temperature $^\circ \mathrm{C}$
> In defaulterrorcallback (line 12)
In matlab.graphics.axis.decorator.DatetimeRuler.get.TickLabelFormat
In matlab.graphics.axis.decorator/DatetimeRuler/format
In matlab.graphics.internal.makeNonNumeric
In matlab.graphics.internal.makeNonNumeric
In n02 (line 33)
  댓글 수: 11
Jon
Jon 2022년 5월 12일
편집: Jon 2022년 5월 12일
Great job isolating the problem!
It looks like, for whatever reason, the raw date strings sometimes have less decimal places. So, the + character appears sooner. Cutting of at character 27 can therefore sometimes include a + in the string which leads to the NaT.
You could just try using one less decimal place and modify to
TimeStamp = cellfun(@(x)[x(1:10),' ',x(12:26)],...
TimeStampRaw,'uniformOutput',false);
You could also take a more robust approach and make a little function to clean up the strings like this:
...
% end, use cellfun to perform operation on every element in the cell array
TimeStamp = cellfun(@(x)cleanTime(x),...
TimeStampRaw,'uniformOutput',false);
% convert TimeStamp strings to datetime array
t = datetime(TimeStamp)
% plot result
plot(t,iot_battery)
% put function to clean the time stamps at end of script, or nested inside of your main function
function tstmp = cleanTime(str)
% clean up time stamps, get rid of letter T in middle and +00:00 at
% end
parts = strsplit(str,{'T','+'});
tstmp = [parts{1},' ',parts{2}];
end
Dharmesh Joshi
Dharmesh Joshi 2022년 5월 12일
편집: Dharmesh Joshi 2022년 5월 12일
Hi Jon
Yes, I noticed that small difference as well. I will incorporate the clean time Function. But with the previous method, is it really necessary to add the decimal point and value?

댓글을 달려면 로그인하십시오.


Dharmesh Joshi
Dharmesh Joshi 2022년 5월 14일
Hi Jon
I got it working now.
Its ploting well.
At the moment, the data sample are per minute. how can i group all the data per hour and work out the mean or average for that hour?
  댓글 수: 1
Jon
Jon 2022년 5월 16일
편집: Jon 2022년 5월 16일
That's great to hear.
I think to keep this thread clear it would be good to accept my answer rather than accepting your own final note (which should probably just be another comment) as an answer. I think it is possible to modify which answer you accept.
Rather than starting up a new thread here I will anwer your question regarding the grouping of data as a further comment under my original answer

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by