MATLAB: Patch which turns transparent at nan values

nanpatch

Hi y'all,
I'm attempting to plot a confidence interval with patch using hte following function that I wrote:
function ciplot(lower,upper,x,colour, alpha);
% ciplot(lower,upper)
% ciplot(lower,upper,x)
% ciplot(lower,upper,x,colour)
%
% Plots a shaded region on a graph between specified lower and upper confidence intervals (L and U).
% l and u must be vectors of the same length.
% Uses the patch function, not 'area'. Therefore multiple shaded plots
% can be overlayed without a problem. Make them transparent for total visibility.
% x data can be specified, otherwise plots against index values.
% colour can be specified (eg 'k'). Defaults to blue.
% K.Duffy 4/17
if length(lower)~=length(upper)
error('lower and upper vectors must be same length')
end
if nargin<4
colour='b';
end
if nargin<3
x=1:length(lower);
end
% convert to row vectors so fliplr can work
if find(size(x)==(max(size(x))))<2
x=x'; end
if find(size(lower)==(max(size(lower))))<2
lower=lower'; end
if find(size(upper)==(max(size(upper))))<2
upper=upper'; end
p=patch([x fliplr(x)],[upper fliplr(lower)],colour)
p.FaceAlpha=alpha
p.EdgeColor='none'
My issues is that now I have variables, where reasonable projections end, yet other variables continue. I'd like to be able to add a confidence interval (via patch) to the following plot, but have it end around 60 degrees or similar. When I turn part of my ci into nan values patch won't plot. I read the help info on vertices, but it's not entirely clear how it could solve my issue.
for context my ci looks like the following: >> Rci(30:60, 🙂
ans =
-0.1514 -0.0056
-0.0772 0.0939
0.0042 0.2024
0.0917 0.3163
0.1841 0.4309
0.2792 0.5436
0.3764 0.6566
0.4733 0.7714
0.5729 0.8869
0.6700 1.0003
0.7652 1.1140
0.8532 1.2170
0.9374 1.3130
1.0310 1.4278
1.1038 1.5281
1.1407 1.5161
1.2073 1.5959
1.3296 1.8024
1.3073 1.7332
1.3155 1.7288
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
NaN NaN
Thanks a bunch for your thoughts/help! Kate

Best Answer

Try something like this (in subplot(2,1,2):
x = linspace(0, 150); % Create Data

y = sin(2*pi*x/300); % Create Data
figure(1)
subplot(2,1,1)
patch([x fliplr(x)], [y fliplr(y+0.1)], 'r')
axis([0 151 -0.5 1.5])
subplot(2,1,2)
patch([x(x <= 60) fliplr(x(x <= 60))], [y(x <= 60) fliplr(y(x <= 60)+0.1)], 'r')
axis([0 151 -0.5 1.5])