# find a maximum for defined months and between a given times

2 views (last 30 days)
Hicham Belh on 24 Jun 2024
Commented: Matlab Pro on 26 Jun 2024
Hi guys,
i try to find a maximum value in a given year timeserie. the seeked maximum for the months (jan, feb and december) have a given time range and for the months (Sept, oct and November) a different range. (see duration time in the code)
i tried my code but its not working. i guess i have a mistake in integrating the duration indexing and the logical check for the months.
any held how to correct the code will be nice.
thank you
filename='out.xlsx';
out=importdata(filename);
M=month(out.Zeit(2:end));
ax=(M==1 | M==2 | M==12); % if month is januar, feb or december than ax=1
bx=(M==9 | M==10 | M==11);% if month is sept, oct or november than bx=1
tod=timeofday(out.Zeit(2:end));
p1w=(duration('07:45:00'):minutes(15):duration('16:15:00'))'; % fist time range we look for a max for ax
p2w=(duration('16:45:00'):minutes(15):duration('18:30:00'))'; % second time range we look for a max for ax
p3h=(duration('07:45:00'):minutes(15):duration('18:00:00'))'; % fist time range we look for a max for bx
ip1w=find((tod >= p1w(1) & tod <= p1w(end)) | (tod >= p2w(1) & tod <= p2w(end))); % look for the index of p1w and p2w
ip2h=find((tod >= p3h(1) & tod <= p3h(end))); % look for the index of p3h
maxW=max(out.SV(ax(ip1w)==1)); % max if conditions of time range and month are filled for ax
maxH=max(out.SV(bx(ip2h)==1)); % max if conditions of time range and month are filled for bx
maxHLZ=max(maxW,maxH);

Matlab Pro on 25 Jun 2024
I think you have some problems in your code;
1. while chopping Zeit (out.Zeit(2:end)) --> you did not chop SV (out.SV)
2. There is a mis-correlation with the indexes created by: ax(ip1w)==1 or bx(ip2h)==1 with relation to out.SV
I have done some fixes to the code. See if it is OK now..
filename='out.xlsx';
out=importdata(filename);
M=month(out.Zeit(2:end));
ax=(M==1 | M==2 | M==12); % if month is januar, feb or december than ax=1
bx=(M==9 | M==10 | M==11);% if month is sept, oct or november than bx=1
tod=timeofday(out.Zeit(2:end));
p1w=(duration('07:45:00'):minutes(15):duration('16:15:00'))'; % fist time range we look for a max for ax
p2w=(duration('16:45:00'):minutes(15):duration('18:30:00'))'; % second time range we look for a max for ax
p3h=(duration('07:45:00'):minutes(15):duration('18:00:00'))'; % fist time range we look for a max for bx
% ip1w=find((tod >= p1w(1) & tod <= p1w(end)) | (tod >= p2w(1) & tod <= p2w(end))); % look for the index of p1w and p2w
% ip2h=find((tod >= p3h(1) & tod <= p3h(end))); % look for the index of p3h
ip1w=(tod >= p1w(1) & tod <= p1w(end)) | (tod >= p2w(1) & tod <= p2w(end)); % look for the index of p1w and p2w
ip2h=(tod >= p3h(1) & tod <= p3h(end)); % look for the index of p3h
SV = out.SV(2:end); % Chop 1st entry (as done in: out.Zeit(2:end))
b = false(size(ax));
f = find(ip1w); % get specific indexes
idx = ax(f) == 1; % get the indexes of change
b(f(idx)) = 1; % .. and change values
maxW=max(SV(b)); % max if conditions of time range and month are filled for ax
b = false(size(ax));
f = find(ip2h); % get specific indexes
idx = ax(f) == 1; % get the indexes of change
b(f(idx)) = 1; % .. and change values
maxH=max(SV(b)); % max if conditions of time range and month are filled for ax
% maxW=max(SV(ax(ip1w)==1)); % max if conditions of time range and month are filled for ax
% maxH=max(SV(bx(ip2h)==1)); % max if conditions of time range and month are filled for bx
maxHLZ=max(maxW,maxH);
Hicham Belh on 25 Jun 2024
Hi Matlab Pro,
thank you for the response.
u got it. the correlation was not correct and the use of out.Zeit also.
thank you very much for the great support.
Matlab Pro on 26 Jun 2024
@Hicham Belh - happy to assist

### Categories

Find more on Calendar in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!