How to obtain a value from timestamp

Hello everyone,
Is there any method, or way how to obtain a value for example same hour previous day ? Thanks a lot

11 commentaires

See
doc datetime
Walter Roberson
Walter Roberson le 13 Mar 2019
In particular if you have an existing datetime object, T, then T - days(1) would be the timestamp for 24 hours earlier. (Though be careful about leap seconds.)
could I apply that formula for obtaining electricity load for same hour previous day ?
prev24h=load-days(1)
Thanks for pattience!
Walter Roberson
Walter Roberson le 13 Mar 2019
What is your input data structure?
Matthew Clark
Matthew Clark le 13 Mar 2019
date of measurment in datetime in format dd.mm.yyy hh:mm:ss sampling 10min and electricity load in double.
Walter Roberson
Walter Roberson le 13 Mar 2019
If you are sampling every 10 minutes then you have 6 samples per hour, times 24 hours, gives 144 samples per day. So if you are looking at load(K) then the load for 24 hours earlier is load(K-144)
so if I apply that formula below I get duration format for example: 549.55 hr and if I that divide with 24 it will be my value at previous hour on same day? Really appreciate your time to explain me that!
prev24h=load-hours(24);
Unless load is a time (which by the variable name it surely wouldn't appear to be or the chosen name is not very descriptive of the content) it won't be, no.
Matlab will silently cast a double to the duration after doing the arithmetic requested and return a duration but that in this case is probably not what you want.
If you have a datetime value like
>> dt=datetime(2018,11,31,23,10,0);
>> dt=dt-hours(24)
dt =
datetime
30-Nov-2018 23:10:00
>>
then the new datetime value dt _is the same time of day on Nov 30 instead of Nov 31 and is also a datetime, NOT a duration.
I'm guessing that's what you're looking for.
Walter is showing you how, if you have data collected on a 10-minute sampling period to compute the location in an array by indexing operations since there a six samples/hour at 10 minute sampling, but that's not computing the actual time, just using what is known about the storage given the sample rate.
Matthew Clark
Matthew Clark le 13 Mar 2019
load (represents electricity load in kW) is in double format.
dpb
dpb le 13 Mar 2019
Modifié(e) : dpb le 14 Mar 2019
BTW NB: load is a builtin (and pretty important) Matlab function to read .mat files. Aliasing it with a variable name, while quite an appropriate name for the application, is probably not a good idea. Since ML is case-sensitive, Load or some other variation would be a likely candidate.
Digression aside, adding a duration to an energy value is then inconsistent in units and simply wrong as I presumed.
It's not clear exactly what you think that should do, but as noted, you either compute the relative position of a value in an array by knowing the offset from present position as Walter shows, or you use the actual time values and look for matching timestamps of that which you wish and then use those indices to retrieve the data.
Again, we're lacking context for how you've got the data stored so is very difficult to write any specific code and talking in generalities is fraught with confusion and imprecision when it comes to writing code.
There is a timetable class in Matlab; you might find it an appropriate container for your purposes. See
doc timetable % for details, examples, etc., ...
Walter Roberson
Walter Roberson le 13 Mar 2019
For any given location, K, in your load data, associated with a particular time, the location (K-144) is the data for the time exactly one day earlier.

Connectez-vous pour commenter.

Réponses (1)

Matthew Clark
Matthew Clark le 14 Mar 2019

0 votes

so if I want to get prev24h value I have to use for loop to loop throughout the array and array at indek i-144 will be my previous value ?

7 commentaires

That would be one way of doing things. Depending what you are doing with the data, you might be able to vectorize, perhaps something like
LD = LoadData(145:end);
LD24 = LoadData(1:end-144+1);
LDdiff = LD - LD24;
Matthew Clark
Matthew Clark le 14 Mar 2019
the problem is that, that I want to include previous day electricity load at same time as input for my NN to improve predictions and if I calculate with formula above I get smaller matrix by -144 so do I have to compensate the length of my matrix ? it is quite uncertain to me. Sorry for that.
dpb
dpb le 14 Mar 2019
You need to "show your work!" We're just shooting in the dark trying to quess what you've actually done and are trying to do...
But, as a general rule, yes, if you are after a set of data that includes load from any point in time and that for the same time but a day earlier, then the composite data set can only include as real data from the prior day forward--iow, a composite dataset can only be one day less than the current dataset.
The only alternative to that would be to indicate missing values by augmenting the prior day with NaN or the like but that probably will not help your estimation process to do so.
here you can se creation of test set my target data is norm_elLoad and TEST matrix is input data aswell. To improve prediction I need to include value at same point on day before like 144(24h before) or 1008(168h week before).
%reading xlsx file columns
raw=readtable('rawData.xlsx'); %rawData.xlsx contains one year measured data every 10 minutes
datum=raw(:,1); %datetime format dd.mm.yyy hh:mm:ss
datumN=table2array(datum);
temperature=raw(:,2);
temperatureN=table2array(temperature);
elLoad=raw(:,4);
%normalising
elLoadN=table2array(elLoad);
hoursN=hour(datumN);
d=day(datumN,'dayofyear');
normD=d/max(d);
months=month(datumN);
monthsN=months/max(months);
nonWorking=isweekend(datum);
morningPeak=(6<=hoursN) & (9>=hoursN);
norm_elLoad=(elLoadN-min(elLoadN))/(max(elLoadN)-min(elLoadN));
normTemperature=(temperatureN-min(temperatureN))/(max(temperatureN)-min(temperatureN));
%calling scripts
mon_sun; %output is matrix of mon to sunday 0-1 values
transform; %sin and cos transform 2*pi*hoursN/max(hoursN)
summer; %summer months
winter; %winter months
autumn; %autumn
spring; %spring
%input matrix
TEST=[normDSIN;normDCOS;monthsNSIN;monthsCOS;hoursNSIN;hoursNCOS;normTemperature';morningPeak';nonWorking';mon_sun;winter;spring;summer;autumn];
Matthew Clark
Matthew Clark le 15 Mar 2019
Modifié(e) : Matthew Clark le 15 Mar 2019
Mr. Walter Roberson what is the difference between narxnet and distdelaynet ? Could I for example closed loop for my problem ? Thanks for your pattience and advice!
Walter Roberson
Walter Roberson le 16 Mar 2019
Sorry, I am not really familiar with the time based networks.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by