How to get unique timestamps | MATLAB | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
How to get unique timestamps
Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
1
October 3, 2016 - 1:57 am
sp_Permalink sp_Print

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'

Avatar
Adarsh_Murthy

Forum Posts: 59
sp_UserOfflineSmall Offline
2
October 4, 2016 - 12:32 pm
sp_Permalink sp_Print

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?  

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
3
October 4, 2016 - 8:44 pm
sp_Permalink sp_Print

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: cstapels, jacktheripper125
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

Nsneilotof

angieus2

goldiepw11

BinanceoSt

gnarender123@gmail.com

edenderest

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee