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

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 日

1 投票

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 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by