MATLAB: Bound Fill Between Plot Lines

fill

I'm trying to fill between two lines on a plot to create a confidence interval plot around the original data plot. The upper and lower bounds of y have been calculated and exist in 25×1 vectors along with the x and y data. The basic code I have (included below) so far works to an extent, as in it does fill between the lines, however it connects one end of the plot back to the start. I imagine that the data needs bounding in some way as to prevent this, but I am at a complete loss of how to do this. Any help would be greatly appreciated. Thanks.
x_plot = [x_data, fliplr(x_data)];
y_plot = [lower_confidence_band_y, fliplr(upper_confidence_band_y)];
hold on
plot(x, y_data);
fill(x_plot, y_plot);
hold off

Best Answer

‘it connects one end of the plot back to the start’
You must have a duplicated data value at the beginning or end of whatever part of the plot is causing the problem. The following code works for me without the error you reported:
x_data = linspace(0, 10, 25);
y_data = 5 + 2*x_data + 0.01*randn(size(x_data));
lower_confidence_band_y = y_data - 2;
upper_confidence_band_y = y_data + 2;
x_plot = [x_data, fliplr(x_data)];
y_plot = [lower_confidence_band_y, fliplr(upper_confidence_band_y)];
figure(1)
plot(x_data, y_data)
hold on
fill(x_plot, y_plot, 'g', 'FaceAlpha',0.5)
hold off
EDIT Added code with solution.
I would keep them all as column vectors, then concatenate them in ‘x_plot’ and ‘y_plot’. (Both versions of those vectors produce the same result.)
This Works
x_plot = [x_data; flipud(x_data)]; % Original Column Vectors

y_plot = [lower_confidence_band_y; flipud(upper_confidence_band_y)]; % Original Column Vectors
x_plot = [x_data' fliplr(x_data')]; % Transposed To Row Vectors

y_plot = [lower_confidence_band_y' fliplr(upper_confidence_band_y')]; % Transposed To Row Vectors
figure(1)
plot(x_data, y_data)
hold on
patch(x_plot, y_plot, 'g', 'FaceAlpha',0.3, 'EdgeColor','g', 'EdgeAlpha',0.8)
hold off
I added some tweaks to the patch call to make the plot a little easier to see. Experiment to get the result you want.