How to extract certain values in an array

12 views (last 30 days)
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
David Hill
David Hill on 1 Nov 2021
This is not difficult. Attaching your data (sample) would be helpful.
Vishal Varadraj
Vishal Varadraj on 1 Nov 2021
Edited: Vishal Varadraj on 1 Nov 2021
@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

Adam Danz
Adam Danz on 1 Nov 2021
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
Vishal Varadraj
Vishal Varadraj on 3 Nov 2021
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));
Adam Danz
Adam Danz on 3 Nov 2021
Thanks, I fixed it. It should be, max(count).

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!