MATLAB: How to shade area under the curve between two X axis indices

plotting

I have a plot of data, and several X axis indices that I want to shade the area under the curve between. How do I do this? I have looked at the fill and area functions but none seem to work for what I am trying and there is no simple way to say shade up to my data line with these x axis bounds.

Best Answer

I am guessing what you want.
Try this:
x = linspace(0, 10*pi); % Create Data

y = sin(x) + sin(x/(3.5*pi)); % Create Data
xidx = [10 15 22 31 65 80]; % Create Index Vector
figure(1)
plot(x, y, '-b')
hold on
for k1 = 1:2:length(xidx)
patch([x(xidx(k1):xidx(k1+1)) fliplr(x(xidx(k1):xidx(k1+1)))], [y(xidx(k1):xidx(k1+1)) zeros(size(xidx(k1):xidx(k1+1)))], rand(1,3))
end
hold off
grid
The rand call sets random colors for the patches. Choose the colors you want for them.