This code section is hard-wired into set years, can I get some tips to make it work automatically if I adjust the years?

조회 수: 15 (최근 30일)
%%
% ------------------ USER INPUT ------------------
start_jahr = 2013; % desired start year
end_jahr = 2022; % desired end year
% -------------------------------------------------
% Determine maximum available index
i_max = min(length(data), length(zeitreihe));
% Basis year/index mapping
basis_jahr = 2015;
basis_index = 76;
i_start = basis_index + (start_jahr - basis_jahr);
% Calculate i_end safely
i_end = i_start + (end_jahr - start_jahr);
if i_end > i_max
warning('Requested end year exceeds available data. Adjusting automatically.');
i_end = i_max;
end
% Number of years to plot
iN = i_end - i_start + 1;
% Adjust years vector
years = start_jahr:(start_jahr + iN - 1);
D = ones(size(years));
% Create datetime ticks safely
if length(years) > 1
t = sort([datetime(years,11,D), datetime(years(2:end),5,D(2:end))]);
else
t = datetime(years,11,D);
end
t = t'; % row vector
% Adjust figure width
baseWidth = 950;
width = round(baseWidth * (iN/9));
figure('Position',[100 100 width 300])
% ------------------ PLOTTING ------------------
tIdx = 1;
for ii = i_start:i_end
% Plot data curve
area(zeitreihe(ii).ts, zeitreihe(ii).wert,'FaceColor','#0072BD','FaceAlpha',0.5,'EdgeAlpha',0.5); hold on;
% Single values for patches
WiMval = mean(data(ii).WiM);
SoMval = mean(data(ii).SoM);
% Check that tIdx+1 and tIdx+2 exist before using fill
if tIdx+1 <= length(t)
fill([t(tIdx) t(tIdx+1) t(tIdx+1) t(tIdx)], [0 0 WiMval WiMval], ...
[0.4660 0.6740 0.1880], 'FaceAlpha',0.5,'EdgeAlpha',0.2);
end
if tIdx+2 <= length(t)
fill([t(tIdx+1) t(tIdx+2) t(tIdx+2) t(tIdx+1)], [0 0 SoMval SoMval], ...
[0.9290 0.6940 0.1250], 'FaceAlpha',0.5,'EdgeAlpha',0.2);
end
tIdx = tIdx + 2;
end
% ------------------ AXES ------------------
set(gca,'XTick',t,'XTickLabel',datestr(t,'mmm'));
set(gca,'XTickLabel',[]);
xtickangle(90);
ylabel('Q in m^3/s')
% ------------------ ANNOTATIONS (dynamic) ------------------
% Horizontal positions scaled to number of years
WIdx_start = 0.05; % left margin
SIdx_start = 0.05 + 0.5/iN; % slightly offset from W
W_width = 0.9 / iN; % width per year
for ii = 1:iN
data_idx = i_start - 1 + ii; % index in data array
if data_idx > length(data)
break; % safety check
end
% Winter Half-Year (WH)
annotation(gcf,'textbox',[WIdx_start + (ii-1)*W_width 0.01 W_width 0.08], ...
'VerticalAlignment','middle', ...
'String',{'WH',[num2str(start_jahr-1+ii) '/' num2str(start_jahr+ii)]}, ...
'HorizontalAlignment','center','FontWeight','bold','FontSize',8,'FitBoxToText','off','EdgeColor','none');
annotation(gcf,'textbox',[WIdx_start + (ii-1)*W_width 0.10 W_width 0.05], ...
'VerticalAlignment','middle', ...
'String',num2str(round(mean(data(data_idx).WiM))), ...
'HorizontalAlignment','center','FontSize',8,'FitBoxToText','off','EdgeColor','none');
% Summer Half-Year (SH)
annotation(gcf,'textbox',[SIdx_start + (ii-1)*W_width 0.01 W_width 0.08], ...
'VerticalAlignment','middle', ...
'String',{'SH',num2str(start_jahr+ii)}, ...
'HorizontalAlignment','center','FontWeight','bold','FontSize',8,'FitBoxToText','off','EdgeColor','none');
annotation(gcf,'textbox',[SIdx_start + (ii-1)*W_width 0.10 W_width 0.05], ...
'VerticalAlignment','middle', ...
'String',num2str(round(mean(data(data_idx).SoM))), ...
'HorizontalAlignment','center','FontSize',8,'FitBoxToText','off','EdgeColor','none');
end
% ------------------ SAVE FIGURE ------------------
savefig(gcf,'OberwasserzuflussAbb3.fig');
print(gcf,'-dpng','OberwasserzuflussAbb3.png');
disp('Grafik wird gespeichert als OberwasserzuflussAbb3.*fig/png')
  댓글 수: 4
Mathieu NOE
Mathieu NOE 2025년 9월 24일 12:19
can you sahre the data so we can try on our side
can you also provide a copy of the "bad" plot with your new parameters
all the best
dpb
dpb 2025년 9월 24일 12:43
편집: dpb 2025년 9월 24일 15:02
% Adjust figure width
baseWidth = 950;
width = round(baseWidth * (iN/9));
figure('Position',[100 100 width 300])
The above and lots of other code is based on fixed assumptions about sizes and the "9" is based on the fixed number of years. Start by changing it to however many years are in the new range of years and see where that leads.
The fixed baseWidth may also be an issue as number of years goes up; you'll just have to see.
Undoubtedly it will take some fiddling and experimentation to work out the spacings if don't just let MATLAB do its thing automagically.
As @Mathieu NOE requests, you'll need to attach a ..mat file with the needed data if you would expect somebody to actually try to muck about with the code itself as well as a sample "correct" image for reference. At a minimum I see, will need the two variables in RHS of
i_max = min(length(data), length(zeitreihe));
Have you first tested that simply changing the year values but keeping the same total number of years does work as expected to ensure there isn't something also dependent upon the magnitude of the years besides just how many there are? The following code, for example, presumes something about what the data structure is to index into the data based on year; your new data will have to have identically the same structure or you'll have to fixup this calculation to match the new data or you'll not be plotting the data you think you are.
% Basis year/index mapping
basis_jahr = 2015;
basis_index = 76;
i_start = basis_index + (start_jahr - basis_jahr);

댓글을 달려면 로그인하십시오.

채택된 답변

janas
janas 2025년 9월 29일 9:53
I adjsuted it (with help of Answers and AI). Thank you everyone
start_jahr = str2double(WhSh_startjahr);
end_jahr = str2double(WhSh_endjahr);
i_max = min(length(data), length(zeitreihe));
basis_jahr = 2015;
basis_index = 76;
i_start = basis_index + (start_jahr - basis_jahr);
i_end = i_start + (end_jahr - start_jahr);
if i_end > i_max
warning('Requested end year exceeds available data. Adjusting automatically.');
i_end = i_max;
end
iN = i_end - i_start + 1;
years = start_jahr:(start_jahr + iN - 1);
figure('Position',[100 100 1200 600])
hold on
idx = i_start;
x = datenum(zeitreihe(idx).ts);
y = double(zeitreihe(idx).wert);
hArea = area(x, y, 'FaceColor','#0072BD','FaceAlpha',0.4,'EdgeAlpha',0.4);
for ii = 1:iN
idx = i_start + ii - 1;
if ii > 1
x = datenum(zeitreihe(idx).ts);
y = double(zeitreihe(idx).wert);
area(x, y, 'FaceColor','#0072BD','FaceAlpha',0.4,'EdgeAlpha',0.4);
end
WiMval = mean(data(idx).WiM,'omitnan');
SoMval = mean(data(idx).SoM,'omitnan');
winterStart = datetime(years(ii)-1,11,1);
winterEnd = datetime(years(ii),4,30);
summerStart = datetime(years(ii),5,1);
summerEnd = datetime(years(ii),10,31);
wX = datenum([winterStart winterEnd winterEnd winterStart]);
sX = datenum([summerStart summerEnd summerEnd summerStart]);
if ii == 1
hWinter = fill(wX, [0 0 WiMval WiMval], [0.4660 0.6740 0.1880], 'FaceAlpha',0.3,'EdgeAlpha',0.2);
hSummer = fill(sX, [0 0 SoMval SoMval], [0.9290 0.6940 0.1250], 'FaceAlpha',0.3,'EdgeAlpha',0.2);
else
fill(wX, [0 0 WiMval WiMval], [0.4660 0.6740 0.1880], 'FaceAlpha',0.3,'EdgeAlpha',0.2);
fill(sX, [0 0 SoMval SoMval], [0.9290 0.6940 0.1250], 'FaceAlpha',0.3,'EdgeAlpha',0.2);
end
winterMid = datenum(winterStart + calmonths(3));
summerMid = datenum(summerStart + calmonths(3));
yl = ylim;
text(winterMid, yl(1) + 0.05*(yl(2)-yl(1)), sprintf('%.0f',WiMval), ...
'HorizontalAlignment','center','FontSize',8,'Color','k');
text(summerMid, yl(1) + 0.05*(yl(2)-yl(1)), sprintf('%.0f',SoMval), ...
'HorizontalAlignment','center','FontSize',8,'Color','k');
end
xlim([datenum(datetime(start_jahr-1,11,1)) datenum(datetime(end_jahr,10,31))])
datetick('x','yyyy','keeplimits');
ylabel('Q in m^3/s')
grid on
box on
legend([hArea, hWinter, hSummer], {'Abfluss','WH','SH'}, 'Location','northwest');
savefig(gcf,'OberwasserzuflussAbb3.fig');
print(gcf,'-dpng','OberwasserzuflussAbb3.png');
disp('Grafik wird gespeichert als OberwasserzuflussAbb3.*fig/png')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by