Problem with fill()/patch() between two lines in R2023a

조회 수: 8 (최근 30일)
Lorenzo Tortora
Lorenzo Tortora 2023년 11월 2일
댓글: Star Strider 2023년 11월 2일
I have a problem with the fill()/patch() command when plotting experimental data.
I want to plot a sine wave with an atan background, and i want to make a picture in which the filled region is the one between the two waves. However, when i attempt to fill() (or patch()) i get a straigth line going from the first to the last point in the functions i create, and i don't know how to avoid this.
What am i doing wrong?
Here is the code for the plot:
%define the variable
x = 0:0.05:6.28
x = 1×126
0 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000 1.0500 1.1000 1.1500 1.2000 1.2500 1.3000 1.3500 1.4000 1.4500
x = transpose(x)
x = 126×1
0 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500
bg = atan(-x)
bg = 126×1
0 -0.0500 -0.0997 -0.1489 -0.1974 -0.2450 -0.2915 -0.3367 -0.3805 -0.4229
linetofill = sin(x)
linetofill = 126×1
0 0.0500 0.0998 0.1494 0.1987 0.2474 0.2955 0.3429 0.3894 0.4350
bg
bg = 126×1
0 -0.0500 -0.0997 -0.1489 -0.1974 -0.2450 -0.2915 -0.3367 -0.3805 -0.4229
%plot raw spectrum
plot(x, linetofill)
hold on
%fill the lines up to the background contribution
fill([x fliplr(x)], [linetofill fliplr(bg)], 'cyan', "FaceAlpha", 0.5)
hold off
  댓글 수: 1
Stephen23
Stephen23 2023년 11월 2일
Have a think about what FLIPLR does to a column vector.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2023년 11월 2일
I am not certain what result you want.
For column vectors, use flip (or flipud) and concatenate the vector arguments vertically —
%define the variable
x = 0:0.05:6.28;
x = transpose(x);
bg = atan(-x);
linetofill = sin(x);
% bg
%plot raw spectrum
plot(x, linetofill)
hold on
%fill the lines up to the background contribution
patch([x; flip(x)], [linetofill; flip(bg)], 'cyan', "FaceAlpha", 0.5)
hold off
Is this what you want? (The fill function produces the same result. Use the function you are most comfortable wiith.)
.
  댓글 수: 2
Lorenzo Tortora
Lorenzo Tortora 2023년 11월 2일
Yes, this is what i wanted to do, sorry for my poor wording. It was both a matter of using the wrong flip function and a missing ";" in the square brackets, i.e. i was writing
patch([x flip(x)], [linetofill flip(bg)]
in my code instead of
patch([x; flip(x)], [linetofill; flip(bg)]
Which i believed produced the unwanted results.
Thanks a lot for the help!
Star Strider
Star Strider 2023년 11월 2일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by