1
votes

I'm trying to create a time vector for a whole week with a 15 minutes step. I want to plot it later on with my data. I managed to create a single day 15 minutes step vector, but when I plot it with my data I can't (its length is 95 instead of 96).

 dv = 1/24:1/96:24/24+2/96;
 datestr(dv)

Also, I wan't to show the date, and time in the format 'dd:MM hh:mm). Here is my code at the moment, I'm getting an error in bsxfun, it says Operands must be numeric arrays.

    t1 = datetime(2013,11,1,8,0,0,'Format','MM:dd: HH:mm:ss');
    t2 = datetime(2013,11,7,8,0,0,'Format','MM:dd: HH:mm:ss')
    days = t1:t2
    % days = days(~ismember(weekday(days),[1 7]));
    out  = bsxfun(@plus, days, dv.');
    datestr(out)

finally, when I get the vector for the whole week, it's a string format, how can I convert it to numbers and plot it? It would be great if you provided me with the right references to guide me as I'm new in this field.

Thanks

1

1 Answers

2
votes

To the first part: If you want to create the time windows of a day i would recommend you start your 15 minute windows at 00:00 (12:00 AM) and end them at 23:45 (11:45 PM) so they are all on the same date. As a vector you would need the numbers starting at 0 and going to 1-(1/96) as 1 is a whole day any you dont want the 00:00 of the next day as 97th value. This is espacially important if you want to put together multiple days so you dont have overlapping.

dv = 0:1/96:1-1/96;
datestr(dv)

To the second part i strongly recommend you look into datetime from MATLAB (by typing help datetimeand then clicking on 'Reference page for datetime' if you are new) and read that part and the references to duration, because i find those work together great. You can use minus or plus if you want to make a datetime earlier/later and you can compare (<) to check which value is earlier and things like that. It is a good documentation with examples. In there you find how to create arrays like this. I am not sure how you came to bsxfunand i dont know if it yould work, but i would recommend a different approach.

t1 = datetime(2013,11,1,8,0,0,'Format','MM:dd: HH:mm:ss'); % as before
t2 = datetime(2013,11,8,8,0,0,'Format','MM:dd: HH:mm:ss'); % changed the date +1 
dur1 = duration(0,15,0); % a 'duration' type with the value of 15minutes

t2=t2-dur1; %taking 15 minutes of the t2 to get rid of the 00:00 of the last day
days = t1:dur1:t2; % from t1 to t2 in 15minute-Steps 96per day*7days should be 

plot(days,rand(length(days),1)); % you can use it directly for plotting
datestr(days) %only if you want to write it to a file 

If you use datestr your datetime becomes a string which you have to do if you want to write it to a file (maybe not even then if you output to excel i think), but then you cant 'work' with the datetimes anymore.

I hope this helps you to get started