how would i use the fill command to fill in between these two matrices

y=[0,1;2,2;2,3;3,5]
xlimit=y(:,1);
ylimit=y(:,2);
x=[0,5;1,5;2,5;3,5]
boundryxlim=x(:,1);
boundryylim=x(:,2);
plot(xlimit,ylimit, 'b' )
hold on
plot(boundryxlim,boundryylim, 'r')

 Accepted Answer

To use fill (and its related functions), you need to outline the first curve, then flip the second curve, providing a ‘closed path’ (that the function itself will then complete if necessary) to outline them correctly. Since your vectors are all column vectors here, you need to vertically concatenate the first vectors and their flipped second vectors.
Try this:
y=[0,1;2,2;2,3;3,5];
xlimit=y(:,1);
ylimit=y(:,2);
x=[0,5;1,5;2,5;3,5];
boundryxlim=x(:,1);
boundryylim=x(:,2);
figure
fill([xlimit; flipud(boundryxlim)], [ylimit; flipud(boundryylim)], 'g')
hold on
plot(xlimit,ylimit, 'b' )
plot(boundryxlim,boundryylim, 'r')
hold off

7 Comments

Thank you star rider learnt a lot from your answer
Thanks for the help. really appreciate it.
One more question if you don't mind. Is there a way to specify a certain distance I want filled? say I run the program and get this plot, can I specify that I only want the fill from .5 to 2 on the x-axis? and how would I if its possible?
Here is the current code: y=[0,1;2,2;2,3;3,5]; xlimit=y(:,1); ylimit=y(:,2);
x=[0,5;1,5;2,5;3,5]; boundryxlim=x(:,1); boundryylim=x(:,2);
figure fill([xlimit; flipud(boundryxlim)], [ylimit; flipud(boundryylim)], 'g') hold on plot(xlimit,ylimit, 'b' ) plot(boundryxlim,boundryylim, 'r') hold off
It is necessary to create entirely new vectors to do this.
Try this:
y=[0,1;2,2;2,3;3,5];
xlimit=y(:,1);
ylimit=y(:,2);
xlimit = xlimit + (1:numel(xlimit))'*1E-12; % Create Increasing (Non-Duplicated) Values For ‘xlimit’
x=[0,5;1,5;2,5;3,5];
boundryxlim=x(:,1);
boundryylim=x(:,2);
xlimit2 = linspace(0.5, 2)'; % Create ‘xlimit2’ For Interpolation, Transpose To Column Vector
boundryxlim2 = xlimit2; % Create ‘boundryxlim2’ For Interpolation
ylimit2 = interp1(xlimit, ylimit, xlimit2); % Interpolate
boundryylim2 = interp1(boundryxlim, boundryylim, boundryxlim2); % Interpolate
figure
fill([xlimit2; flipud(boundryxlim2)], [ylimit2; flipud(boundryylim2)], 'g')
hold on
plot(xlimit,ylimit, 'b' )
plot(boundryxlim,boundryylim, 'r')
hold off
Adding a vector of small increasing values to a vector with duplicates in order to create a monotonically changing vector is not uniformly applicable. It works when you want to do a linear interpolation, as I do here.
Experiment with this code.
Thanks this is very helpful and I feel like I am learning a lot about fill and Interp1 which is cool. So if I wanted to fill from left to right using this code I could create a new line and use "fliplr" and interp1 with flipped axis to get it too work?
So ive been playing with this and keep getting a weird plot, do you see what I am doing wrong?
Code:
y=[0,1;2,2;2,3;3,5];
xlimit=y(:,1);
ylimit=y(:,2);
ylimit = ylimit + (1:numel(ylimit))'*1E-12 % Create Increasing (Non-Duplicated) Values For ‘ylimit’
z=[3.5,2;3.5,3;3.5,4;3.5,5]
boundryxlim=z(:,1);
boundryylim=z(:,2);
ylimit2 = linspace(2, 3)'; % Create ‘xlimit2’ For Interpolation, Transpose To Column Vector boundryylim2 = ylimit2; % Create ‘boundryxlim2’ For Interpolation xlimit2 = interp1(ylimit, xlimit, ylimit2)
boundryylim2 = interp1(boundryylim,boundryxlim,boundryylim2); % Interpolate figure fill([xlimit2; fliplr(boundryxlim2)], [ylimit2; fliplr(boundryylim2)], 'g') hold on plot(xlimit,ylimit, 'b' ) plot(boundryxlim,boundryylim, 'r') xlim([0 5]) ylim([0 5]) hold off
Don’t ‘fill from left to right’. The patch and related commands require that you define the area you want to colour as a complete, closed polygon. The polygon (x,y) coordinates must be ‘ordered’ in the sense that they outline the area in either a clockwise or anti-clockwise direction.
So if I am trying to fill on the other side. I will create a new boundary and interpolate it using y coordinates, is this correct? It seems close but I am not sure. This is what I have
if true
y=[0,1;2,2;2,3;3,5];
xlimit=y(:,1);
ylimit=y(:,2);
ylimit = ylimit + (1:numel(ylimit))'*1E-12 % Create Increasing (Non-Duplicated) Values For ‘xlimit’
x=[3.5,1.5;3.5,2.5;3.5,5;3.5,4;3.5,4.5];
boundryxlim=x(:,1);
boundryylim=x(:,2); % code
ylimit2 = linspace(1, 2.5)' % Create ‘xlimit2’ For Interpolation, Transpose To Column Vector
boundryylim2=ylimit2; % Create ‘boundryxlim2’ For Interpolation
xlimit2 = interp1(ylimit, xlimit, ylimit2) % Interpolate
boundryxlim2 = interp1(boundryylim, boundryxlim, boundryylim2) % Interpolate
figure
fill([xlimit2; flipud(boundryxlim2)], [ylimit2; flipud(boundryylim2)], 'g')
hold on
plot(xlimit,ylimit, 'g' )
plot(boundryxlim,boundryylim, 'r')
hold off
end
I cannot follow what you want to do. If I assume correctly, these will work, all of them slight variations on the original idea for filling the centre (included here):
y=[0,1;2,2;2,3;3,5];
xlimit=y(:,1);
ylimit=y(:,2);
xlimit = xlimit + (1:numel(xlimit))'*1E-12; % Create Increasing (Non-Duplicated) Values For ‘xlimit’
x=[0,5;1,5;2,5;3,5];
boundryxlim=x(:,1);
boundryylim=x(:,2);
% —————————— FILL CENTRE ——————————
xlimit2 = linspace(0.5, 2)'; % Create ‘xlimit2’ For Interpolation, Transpose To Column Vector
boundryxlim2 = xlimit2; % Create ‘boundryxlim2’ For Interpolation
ylimit2 = interp1(xlimit, ylimit, xlimit2); % Interpolate
boundryylim2 = interp1(boundryxlim, boundryylim, boundryxlim2); % Interpolate
figure
fill([xlimit2; flipud(boundryxlim2)], [ylimit2; flipud(boundryylim2)], 'g')
hold on
plot(xlimit,ylimit, 'b' )
plot(boundryxlim,boundryylim, 'r')
hold off
% —————————— FILL LEFT ——————————
xlimit3 = linspace(0, 0.5)'; % Create ‘xlimit3’ For Interpolation, Transpose To Column Vector
boundryxlim3 = xlimit3; % Create ‘boundryxlim3’ For Interpolation
ylimit3 = interp1(xlimit, ylimit, xlimit3, 'linear','extrap'); % Interpolate
boundryylim3 = interp1(boundryxlim, boundryylim, boundryxlim3); % Interpolate
figure
fill([xlimit3; flipud(boundryxlim3)], [ylimit3; flipud(boundryylim3)], 'g')
hold on
plot(xlimit,ylimit, 'b' )
plot(boundryxlim,boundryylim, 'r')
hold off
% —————————— FILL RIGHT ——————————
xlimit4 = linspace(2, 3)'; % Create ‘xlimit4’ For Interpolation, Transpose To Column Vector
boundryxlim4 = xlimit4; % Create ‘boundryxlim4’ For Interpolation
ylimit4 = interp1(xlimit, ylimit, xlimit4, 'linear','extrap'); % Interpolate
boundryylim4 = interp1(boundryxlim, boundryylim, boundryxlim4); % Interpolate
figure
fill([xlimit4; flipud(boundryxlim4)], [ylimit4; flipud(boundryylim4)], 'g')
hold on
plot(xlimit,ylimit, 'b' )
plot(boundryxlim,boundryylim, 'r')
hold off
Note that they each simply change the original ‘xlimit’ variations vector (such as ‘xlimit3’ and the others). It is necessary to use the 'extrap' option to define the corresponding ‘ylimit3’ to avoid NaN values that will not plot.
Each plots in a separate figure.

Sign in to comment.

More Answers (0)

Categories

Find more on Elementary Math in Help Center and File Exchange

Tags

Asked:

Pax
on 30 Jul 2018

Commented:

on 3 Aug 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!