how would i use the fill command to fill in between these two matrices
6 views (last 30 days)
Show older comments
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')
0 Comments
Accepted Answer
Star Strider
on 30 Jul 2018
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
Star Strider
on 3 Aug 2018
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.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
