This code section is hard-wired into set years, can I get some tips to make it work automatically if I adjust the years?
    4 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
%%
% ------------------ 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 Commenti
  Mathieu NOE
      
 il 24 Set 2025
				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
      
      
 il 24 Set 2025
				
      Modificato: dpb
      
      
 il 24 Set 2025
  
			% 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);
Risposta accettata
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Bar Plots in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



