How to extract certain values in an array

I have a vector containing n rows (dependent on user input) and two columns. The first column is the time every minute and the second being the power at each time. I want to find the power from 08:00 to 17:00 everyday. Picture below shows this array. I converted the char array to a double thinking it could be easier. I would have to do this for all the days in the month or could be from the 15th of one month to the 18th of the next. I also have the datenum values for the time as shown below. I feel using this would be better as it would uniquely identify each day, minute and month without using a numerical method ie using modulus to find the first two values in the cell and sprintf. I've also attached a sample of my code where SM,SD,SH,EM,ED,EH are Start month, day, hour and End Month, Day, Hour given by the user. I have a database of the weather files which contains the outside air temp by the hour from which I find the power out at each minute. Start and end time defined by the user ex 062311 meaning the 6th month, 23rd day and 11th hour.
%% Extracts Required Weather Temperature
m = floor(log10(Start));
D = mod(floor(Start ./ 10 .^ (m:-1:0)), 10);
if length(D) == 5
SM = D(1);
str = sprintf('%d%d%d', D(2), D(3));
SD= str2double(str);
str = sprintf('%d%d%d', D(4), D(5));
SH= str2double(str);
else
str = sprintf('%d%d%d', D(1), D(2));
SM= str2double(str);
str = sprintf('%d%d%d', D(3), D(4));
SD= str2double(str);
str = sprintf('%d%d%d', D(5), D(6));
SH= str2double(str);
end
%% Getting Time Values for the Plot
t1 = datetime(Year(1),SM,SD,SH,0,0);
t2 = datetime(Year(1),EM,ED,EH,1,0);
t11=datevec(datenum(t1));
t22=datevec(datenum(t2));
time_interval = etime(t22,t11)/60;
for c = 1:time_interval
time(c) = t1 +minutes(c);
end
c=datestr(time, 'mm dd HH:MM');
timestamp=datenum(c, 'mm dd HH:MM');
Thanks in Advance

2 Comments

This is not difficult. Attaching your data (sample) would be helpful.
@David Hill Sure, I have attached my workspace containg the double column vector, the char array, datenum array. Let me know if you require anything else.

Sign in to comment.

 Accepted Answer

Use datetime values instead of any other form of date/time. That allows you to easily index by hour etc.
% Load the data
data = load('matlab3.mat');
% Convert timestamp to datetime values
dtStamp = datetime(data.timestamp,'ConvertFrom','datenum')
dtStamp = 44581×1 datetime array
23-Jun-2021 01:01:00 23-Jun-2021 01:02:00 23-Jun-2021 01:03:00 23-Jun-2021 01:04:00 23-Jun-2021 01:05:00 23-Jun-2021 01:06:00 23-Jun-2021 01:07:00 23-Jun-2021 01:08:00 23-Jun-2021 01:09:00 23-Jun-2021 01:10:00 23-Jun-2021 01:11:00 23-Jun-2021 01:12:00 23-Jun-2021 01:13:00 23-Jun-2021 01:14:00 23-Jun-2021 01:15:00 23-Jun-2021 01:16:00 23-Jun-2021 01:17:00 23-Jun-2021 01:18:00 23-Jun-2021 01:19:00 23-Jun-2021 01:20:00 23-Jun-2021 01:21:00 23-Jun-2021 01:22:00 23-Jun-2021 01:23:00 23-Jun-2021 01:24:00 23-Jun-2021 01:25:00 23-Jun-2021 01:26:00 23-Jun-2021 01:27:00 23-Jun-2021 01:28:00 23-Jun-2021 01:29:00 23-Jun-2021 01:30:00
% Extract the hour of each timestamp
hr = hour(dtStamp); % numeric integers 0:23
hr = 44581×1
1 1 1 1 1 1 1 1 1 1
% Index hours between 8 and 17
idx = hr>8 & hr<17;
I don't know how you're planning on using the results but idx is a logical index identifying the rows of data that are between the specified hours.

4 Comments

Thanks for your reply!! That was useful. I was struggling to get the hour from the time values. I was wondering if there would be any way of grouping the hours by the date. Ex: 8-17:00 for the 24th, 8-17:00 for the 25th, etc. Purely because id have to take the area underneath the power curve for those limts and throw them onto a histogram or bar chart by date. I've done this so far but it would be very helpful if I could group them by date.
%Finds the required the hours
hr = hour(dtStamp);
idx(:,1) = hr>=8 & hr<17;
%Indexes the required timestamp values and creates a new array with only
%the required values
C = timestamp(idx(:,1));
Newdtstamp = datetime(C,'ConvertFrom','datenum');
[Lia, LocB] = ismember(Newdtstamp,dtStamp);
%Creates a table with the datetime, timestamp and power values
for n = 1:length(LocB)
NewArray1(n) = datetime(timestamp(LocB(n)),'ConvertFrom','datenum');
NewArray2(n) = timestamp(LocB(n));
NewArray3(n) = power(LocB(n));
NewArrays = table(NewArray1, NewArray2, NewArray3);
end
Tia
This produces a table T that lists Power values for each day (columns). You could add row names for each hour:minute sample if you'd like.
% Load the data
data = load('matlab3.mat');
% Convert timestamp to datetime values
dtStamp = datetime(data.timestamp,'ConvertFrom','datenum');
% Extract the hour of each timestamp
hr = hour(dtStamp); % numeric integers 0:23
% Index hours between 8 and 17
hrIdx = hr>8 & hr<17;
% Count number of data points within hour range for each date
count = diff(find([0,hrIdx(:).',0]==0))-1;
count(count==0) = [];
% List all dates
dates = dateshift(dtStamp,'Start','Day');
datesUnq = unique(dates);
nDates = numel(datesUnq); % number of unique dates
% Loop through each date
% Store Power values without hour range for each date.
% Your data contains 480 values for each date but I
% don't know whether you always expect the same number
% of data points per day so I'll play it safe and use
% a loop.
T = array2table(NaN(max(count),nDates),...
'VariableNames',string(datesUnq));
for i = 1:nDates
tempPwrData = data.Power(dates==datesUnq(i) & hrIdx);
T.(string(datesUnq(i)))(1:numel(tempPwrData)) = tempPwrData;
end
Results:
8×32 table
23-Jun-2021 24-Jun-2021 25-Jun-2021 26-Jun-2021 27-Jun-2021 28-Jun-2021 29-Jun-2021 30-Jun-2021 01-Jul-2021 02-Jul-2021 03-Jul-2021 04-Jul-2021 05-Jul-2021 06-Jul-2021 07-Jul-2021 08-Jul-2021 09-Jul-2021 10-Jul-2021 11-Jul-2021 12-Jul-2021 13-Jul-2021 14-Jul-2021 15-Jul-2021 16-Jul-2021 17-Jul-2021 18-Jul-2021 19-Jul-2021 20-Jul-2021 21-Jul-2021 22-Jul-2021 23-Jul-2021 24-Jul-2021
___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________ ___________
0.27543 0.0014632 0.76627 0.37671 0.68308 0.77939 0.45586 0.88926 0.4558 0.71364 0.09672 0.60518 0.82153 0.32977 0.6725 0.95108 0.14299 0.9399 0.26763 0.4187 0.98687 0.7667 0.05809 0.24959 0.92829 0.08032 0.4165 0.58295 0.65232 0.95283 0.9713 NaN
0.28182 0.88379 0.37418 0.14919 0.42144 0.49486 0.24282 0.34772 0.89447 0.19414 0.45195 0.3145 0.0037918 0.10867 0.25242 0.077994 0.78233 0.50809 0.98607 0.54015 0.014542 0.30645 0.98342 0.65502 0.74644 0.81865 0.68809 0.34146 0.27282 0.9925 0.71201 NaN
0.88007 0.40438 0.18986 0.034097 0.65934 0.76151 0.0019221 0.66672 0.072557 0.58008 0.76195 0.13487 0.66026 0.58502 0.94445 0.43673 0.88102 0.98336 0.89896 0.074818 0.2306 0.19702 0.072581 0.50255 0.35986 0.16084 0.71166 0.83176 0.95671 0.36117 0.48692 NaN
0.44433 0.30121 0.6465 0.78225 0.63343 0.66704 0.61534 0.45446 0.63544 0.24747 0.62583 0.92812 0.63236 0.88608 0.32634 0.78166 0.83031 0.71913 0.63066 0.5704 0.33099 0.50136 0.39437 0.41365 0.37646 0.78049 0.96336 0.86145 0.41529 0.32897 0.86469 NaN
0.75591 0.95058 0.0036053 0.32725 0.52515 0.97811 0.66123 0.44688 0.80862 0.93591 0.0014915 0.95791 0.21048 0.89856 0.93676 0.16364 0.38363 0.71315 0.31033 0.83091 0.24739 0.029427 0.74095 0.76493 0.15339 0.97011 0.40699 0.26119 0.58269 0.6182 0.21146 NaN
0.6033 0.46065 0.28289 0.81748 0.19128 0.11707 0.66034 0.017031 0.3211 0.56456 0.017172 0.98927 0.43495 0.53542 0.26147 0.69284 0.19712 0.29392 0.55893 0.29629 0.46637 0.42884 0.69484 0.25108 0.66415 0.37617 0.52985 0.036062 0.70157 0.50901 0.71237 NaN
0.78327 0.28765 0.6386 0.17362 0.51191 0.86932 0.68048 0.86142 0.53607 0.87961 0.45685 0.22267 0.93311 0.75517 0.84227 0.43468 0.5122 0.94505 0.77421 0.64406 0.17156 0.58227 0.011819 0.25109 0.17885 0.80373 0.70769 0.36814 0.28771 0.56813 0.34383 NaN
0.11393 0.084627 0.59207 0.67612 0.74833 0.56786 0.85065 0.93782 0.86608 0.99268 0.22156 0.36796 0.45728 0.41672 0.65062 0.8199 0.27781 0.40574 0.082168 0.13784 0.9469 0.070604 0.5445 0.49472 0.8826 0.14923 0.88782 0.47107 0.13418 0.90671 0.15452 NaN
Thanks so much! The following line didn't work
T = array2table(NaN(max(d),nDates),...
'VariableNames',string(datesUnq));
I'm not sure what value you are trying to put in for the NaN matrix. What exactly is max(d)? I did however change it use the following line.
[numRows,numCols] = size(Power);
T = array2table(NaN(numRows,nDates),...
'VariableNames',string(datesUnq));
Thanks, I fixed it. It should be, max(count).

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!