MATLAB: How to get dynamic variable background colors

backgroundMATLABpatchpatchesplot

Hi, I have two vectors vec1 and vec2 of the same length that I am plotting in one graph. I want a background color to show up where vec1 has a greater value than vec2. I tried doing this with patches but I can't get the indices right.
h=patch([xcolstart xcolend xcolend xcolstart],[0 0 5 5],'g');
set(h,'FaceAlpha',0.2);
set(h,'EdgeColor','none');
Basically I need the indices of start and end points xcolstart and xcolend in sections where vec1>vec2. Is there an elegant way of doing this or is there another function besides patch that can do this more easily? Thank you

Best Answer

I think I figured it out myself:
bool=1;
for ii=1:numel(vec1)
if(abs(vec1(ii))>abs(vec2(ii)))
if bool==1
xcolstart=ii;
bool=0;
end
else
if bool == 0
xcolend=ii;
h=patch([xcolstart xcolend xcolend xcolstart],[-100 -100 100 100],'g');
set(h,'FaceAlpha',0.2);
set(h,'EdgeColor','none');
bool=1;
end
end
end