0

I have a datetime array that highlights the peaks of a function "datepeak", for every day in one year. I obtained it using a datetime array "date" and the array with the position of the peaks "position".

t1 = datetime(year,1,1,0,0,0);
t2 = datetime(year,12,31,23,59,0);
date = t1:minutes(1):t2;
datepeak=date(position);

I need to take the n number of peaks for the day 1 and transpose this array to the first row of the matrix, and so on. Since the number of peaks are not constants (min 3 max 4) I tried to initiate the matrix like this:

matrix=NaN(365,4)

Then I override the NaN of every row with this double for loop:

for i=1:365
v=datepeak(day(datepeak,'dayofyear')==i);
for c=1:length(v)
    matrix(i,c)=(v(c));
end
end

This loop works (I tried it with the peaks), but with datetime I get an error.

Here's an example to paste:

year=2016;
position=[128 458 950];
t1 = datetime(year,1,1,0,0,0);
t2 = datetime(year,12,31,23,59,0);
date = t1:minutes(1):t2;
datepeak=date(position);

matrix=NaN(365,4);

for i=1:365
v=datepeak(day(datepeak,'dayofyear')==i);
for c=1:length(v)
    matrix(i,c)=(v(c));
end
end
7
  • What error do you get ? Commented May 6, 2017 at 14:17
  • The following error occurred converting from datetime to double: Undefined function 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric, first subtract off a datetime origin, then convert to numeric using the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions. Commented May 6, 2017 at 14:26
  • The following error occurred converting from datetime to double: Undefined function 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric, first subtract off a datetime origin, then convert to numeric using the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions. Commented May 6, 2017 at 14:26
  • I think because the NaN matrix is expecting a double value Commented May 6, 2017 at 14:27
  • Do you absolutely need a 365x4 array ? Would a list of 365 elements, each of them containing the 3 or 4 peaks of the day be ok ? Commented May 6, 2017 at 14:40

1 Answer 1

1

The nan array is of class double whereas datepeak is of class datetime so you can't store them in the same array. The way you represent your data should be driven by what you want to do with them later (and what is feasible). In your case, i'll assume that list 365 elements, containing the (any number) peak times of the day is ok.

year=2016;
position=[128 458 950];
t1 = datetime(year,1,1,0,0,0);
t2 = datetime(year,12,31,23,59,0);
date = t1:minutes(1):t2;
datepeak=date(position);

peaktimes_list = cell(365,1);

for i=1:365
    peaktimes_list{i} = datepeak(day(datepeak,'dayofyear')==i);
end

EDIT : For a 365x4 cell array, change the last part by :

peaktimes = cell(365,4);

for i=1:365
    v = datepeak(day(datepeak,'dayofyear')==i);
    nv = numel(v);
    peaktimes(i,1:nv) = num2cell(v);
end

When there are less than 4 values, the remaining columns will be empty.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you I have to create a table. Once I'll have the matrix with the datetime: result=table(peak(:,1),datematrix(:,1),peak(:,2),datematrix(:,2),peak(:,3),datematrix(:,3),peak(:,4),datematrix(:,4));
I think you intended "peaktimes" and not "peaktimes_list" inside the loop. Anyway it works fine. Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.