MATLAB: Problem with fill / patch

fillMATLABpatch

Hi, I would like to use fill or patch to colour the space between two lines. I have a minimum value and a maximum value and both are plotted against a datetime vector. I have tried to follow carefully other instructions for using patch or fill but I am not getting what I expect – any ideas where I am going wrong? Many thanks!
%With fill and flip lr
x=[1:1:14568]'; %#initialize x array
y1=MC_all_light.SG_MC_light.SN_ICE_D_stats.min; %#create first curve
y2=MC_all_light.SG_MC_light.SN_ICE_D_stats.max; %#create second curve
X=[x,fliplr(x)]; %#create continuous x value array for plotting
Y=[y1,fliplr(y2)]; %#create y values for out and then back
figure();fill(X,Y,'b');
%Basically the same with patch and flipud. Here I am using DateTime, but I get the same results with a vector as for fill.
figure();
patch([datenum(SG_out.DateTime) flipud(datenum(SG_out.DateTime))], [MC_all_light.SG_MC_light.SN_ICE_D_stats.max flipud(MC_all_light.SG_MC_light.SN_ICE_D_stats.min)], [0.6 0.8 1.0])
The first graph is with fill the second with patch. I really want the colour to be between the max and min lines!

Best Answer

It would help to have your data.
Try something like this:
x = linspace(0, 15000, 1000);
y1 = (500*rand(size(x))-1E-5*(7000-x).^2)*1E-2 + 54.5-x*1E-3;
y2 = (500*rand(size(x))-1E-5*(7000-x).^2)*1E-2 + 54.5-2*x*1E-3;
y1y2 = [y1; y2];
figure
plot(x, y1)
hold on
plot(x, y2)
fill([x fliplr(x)], [min(y1y2) fliplr(max(y1y2))], 'b')
hold off
If you want to colour between ther max and min values (and since it appears that both curves have the same number of points), tell fill to do just that! (If you are using datetime arrays, fill is preferable to patch, since patch will not work with datetime arrays.)