MATLAB: How to fill a stairs plot with color

MATLAB and Simulink Student Suitestairs plot color fill

Here is my code:
x = [4 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 2 3 4 3 4 2 3 4 3 4 3 4 2 3 2 3 2 3 2 3 2 3 4 3 4 3 2 4 3 4 3 2 3 2 4 2 1];
y = [0 16 22 27 29 31 39 40 42 46 49 51 53 62 71 77 86 96 99 104 106 107 109 111 114 116 118 119 121 124 127 130 164 174 182 202 221 225 226 227 230 256 269 278 306 342 352 362 371 421 484 498 712 760 764 772 804];
stairs(x,y);
set(gca, 'YDir', 'reverse')
xlim([0 5])
ylim([0 805])
grid on
Here is the figure:
Fig1.jpg
I would like to know how to fill color inside the staris lines. So, for example, if x = 4, fill blue. If x = 3, fill color green. If x = 2 fill color red. If x = 1, fill color yellow.
Any help will be appreciated. Thank you!

Best Answer

If you're trying to fill the left side of the staircase based on the value of x, here's a solution using rectangle(). It assumes y is monotonically increasing. If that's not the case, simply sort x and y so that it is.
colors = {'y' 'r' 'g' 'b'}; %in order of x = 1,2,3,4
xl = xlim();
for i = 1:length(x)-1
rh = rectangle('Position', [xl(1), y(i), x(i+1), (y(i+1)-y(i))]);
rh.EdgeColor = 'none';
rh.FaceColor = colors{x(i+1)};
end
Which produced this figure: