displaying date for x-axis | MATLAB IoT Analytics | 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
displaying date for x-axis
No permission to create posts
April 19, 2019
1:46 pm
Avatar
obod0002

Silver
Members
Forum Posts: 19
Member Since:
March 13, 2019
sp_UserOfflineSmall Offline

Dear MATLAB lovers,
I beg it's easy but I can't get it to work: I only want to display the date on the x-axis.

I'm using data stored by an IOT device. When downloading the data from MATLAB the CSV contains the date, so it must be somewhere ...

Here's my code fragment:
..
Month = thingSpeakRead(readChannelID,'Fields',FieldID,'dateRange',...
[datetime('now')-days(7),datetime('now')],'ReadKey',readAPIKey);
...
minAnzWerte= [length(Month)];
allData=[Month(1:minAnzWerte)];
...
plot((1:minAnzWerte),allData);

Works fine, but -of course- displays numbers for the x-axis.

When I use something like
t1=datenum(datetime('now')-days(7))
t2=datenum(datetime('now'))
t=t1:t2
plot(t,allData);

I'm receiving an error the the Vector lengths don't match ('Error using plot - Vectors must be the same length.'). And MATLAB's right.

Can you please help how I have to assign 'allData' and the plot-command?
Cheers
obod0002

April 19, 2019
3:05 pm
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 861
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

I would recommend using a timetable.

add the name, value pair 'outputFormat', 'timeTable' to the thingSpeakRead function.
You can see that here:
https://www.mathworks.com/help/thingspeak/Use-Area-Plot-to-Compare-Traffic-Data-Sets.html

then

plot(Month.Timestamps,Month.FIELD1LABEL);

should do the trick if you change the FIELD1LABEL to your field 1 label. (case sensitive)

April 20, 2019
11:55 am
Avatar
obod0002

Silver
Members
Forum Posts: 19
Member Since:
March 13, 2019
sp_UserOfflineSmall Offline

Thanks a lot for your help, cstapels. Unfortunately I'm still doing something wrong ...

timeAndData = thingSpeakRead(readChannelID,'Fields',FieldID,'dateRange',...
[datetime('today')-days(7),datetime('today')],'ReadKey',readAPIKey,...
'Outputformat','Timetable');

plot(timeAndData.TimeStamps,timeAndData.Fields);

=>
Error using tst (line 13)
Unrecognized table variable name 'TimeStamps'.

In case I'm using 'Timetable' instead of 'TimeStamps', MATLAB proposes to use the timetable-function.

April 20, 2019
12:57 pm
Avatar
obod0002

Silver
Members
Forum Posts: 19
Member Since:
March 13, 2019
sp_UserOfflineSmall Offline

looks like 'stackedplot(timeAndData);' might be the solution ...

April 23, 2019
8:11 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 861
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Editing the code above - it should be 'Timestamps'. No capital s. My apologies. The stacked plots are nice for timetables, but there are still a few things you might want to do that you need a regular plot for. I leave it to you to decide 🙂

April 26, 2019
2:28 am
Avatar
obod0002

Silver
Members
Forum Posts: 19
Member Since:
March 13, 2019
sp_UserOfflineSmall Offline

Nop, the hint with the timetable was also very helpful. But as I'm seeing the limitations for stacked plots I somehow want to avoid them.
My problem seems to be a basic one: how can I access the data itself? Having something like

FieldID = 4;
voltageWeek1t = thingSpeakRead(readChannelID,'Fields',FieldID,'dateRange',...
[datetime('today')-days(7),datetime('today')],'ReadKey',readAPIKey,...
'Outputformat','Timetable');

results in errors acessing the data itself:
plot(voltageWeek1t.Timestamps,voltageWeek1t.Fields);
=>
Error using tst 4er plot timestamp (line 44)
Unrecognized table variable name 'Fields'.

Same for plot(voltageWeek1t.Timestamps,voltageWeek1t.4);;

Somewhere else I had to use the given name for the field, i.e. '4: Spannung DG' (without the BLANK inbetween) but that doesn't like me either.

April 26, 2019
4:12 am
Avatar
obod0002

Silver
Members
Forum Posts: 19
Member Since:
March 13, 2019
sp_UserOfflineSmall Offline

ooooooops, that's mean ...
MATLAB killed one of my German 'Umlauts' in the table naming although it's displayed correctly in the right column below 'Channel Info'.
How to detect? Use
summary(tablename)
to display what's hidden.

April 26, 2019
9:16 am
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 861
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Thanks for the hint on summary(timetable)
Ill see what is being done about 'Umlauts'.
Does the summary command solve your problem determining the names to access the data for the previous post?

May 2, 2019
6:05 am
Avatar
obod0002

Silver
Members
Forum Posts: 19
Member Since:
March 13, 2019
sp_UserOfflineSmall Offline

yes, looking fine, thanks a lot for your help.
As always: if you get something to work you want more.
At the moment I'm experimenting with multiple plots and how to display the date on the x-axis (e.g. four plots, one week each). Let's see how far I can get. My goal is a plot where I can easily get the information on how much battery capacity has been used over the last few weeks and how much is still left.
Please don't spend to much time on the Umlauts ...

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
16 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:

briskpanda, Dejw0089, Yassine Ben Salah, anis, 43994554AKS, suda

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457