How to get unique timestamps | MATLAB | 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
How to get unique timestamps
No permission to create posts
October 3, 2016
1:57 am
Avatar
rw950431

Top
Members
Forum Posts: 262
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

I have the matlab code below but it sometimes fails because of duplicated time-stamps in the data. (this causes 'bar' to fail with 'XData must be unique' error).

Wondering how to get a unique set of timestamps then rejoin them to the values (I dont particularly care which of the duplicated values gets matched).  My newbie attempts to use the 'unique' function do not work because the row as a whole is considered unique even when the timestamps are the same.

 

02/10/2016 01:30 -436.43
02/10/2016 01:30 -390.61
02/10/2016 01:35 -390.68
02/10/2016 01:35 -390.6

% Draw a bar graph of Interconnector power
% red for positive, blue for negative

power = thingSpeakRead(150393,'OutputFormat','Table','NumDays',3);
pos=unique(power(power.Interconnect > 0,[1,4]));
neg=unique(power(power.Interconnect <= 0,[1,4]))
figure('position', [0, 0, 1200, 700])
bar(datenum(neg.Timestamps),neg.Interconnect,'r','BarWidth',1,'EdgeColor','r')
hold on
bar(datenum(pos.Timestamps),pos.Interconnect,'b','BarWidth',1,'EdgeColor','b')
hold off
datetick('x','ddd dd/mm')
title 'South Australian Interconnector Load'
ylabel 'Net Power (MW)'
legend('Buying Power','Selling Power','Location','northwest')
axis 'tight'

October 4, 2016
12:32 pm
Avatar
Adarsh_Murthy

MathWorks
Members
Forum Posts: 62
Member Since:
August 25, 2015
sp_UserOfflineSmall Offline

Since you are looking to find rows with unique timestamps, call unique() only on Timestamps column. I added a couple lines of code to your snippet and it should work now.

power = thingSpeakRead(150393,'OutputFormat','Table','NumDays',3);
[~, uniqueIndx] = unique(power.Timestamps);
power = power(uniqueIndx, :);
pos= power(power.Interconnect > 0,[1,4]);
neg= power(power.Interconnect <= 0,[1,4]);
<insert the remaining lines of code>

Take a look at the link below for further info about unique() function:

https://www.mathworks.com/help/matlab/ref/unique.html

 

I am curious about the cause for duplicate timestamps. How are you adding data to channel 150393 - from a device, or MATLAB Script tied to a TimeControl/React or another method?  

October 4, 2016
8:44 pm
Avatar
rw950431

Top
Members
Forum Posts: 262
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Thanks Adarsh, that worked perfectly.

The repeated timestamp appears to be an artefact of the start of daylight savings time. The values are coming from a script that scrapes data off a website : the data includes a timestamp so I use 'created_at' to set this time in the thingspeak record.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
23 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

BarryMaync, ThomasRougs, DonaldGen, prince, JrGordon, Elamsweems

Administrators: Hans: 387, lee: 457