How to remove unwanted double image in plot

Hi Fellas
I created this plot from a text file I imported into matlab, and I am not sure why the plot looks like it has these extra lines on it, particularly one going straight from the starting point to the end poin on the graph. I am not sure why this plot has this issue, as I looked through the text file and all of the numbers seem correct. The plot was originally generated using a network analyzer on an antenna, and no double lines were originally present. I have also not seen this issue with any of the other plots I have made in matlab. Any info on what could be the issue is appreciated. I have attached the matlab code file and the s parameter data below.

 Accepted Answer

Matt J
Matt J on 25 Apr 2026 at 23:34
It is possible your points are not sorted in ascending order of x.

7 Comments

Not sure what you mean by ascending order.
Check that your data is sorted by calling issorted on the variable you're using as the X coordinates of your plot.
x = [1 2 3 4 5];
issorted(x, 'ascend') % true, x is in increasing order
ans = logical
1
y = [1 2 3 4 5 1];
issorted(y, 'ascend') % false, y(6) is greater than y(5)
ans = logical
0
plot() connects your (x,y) points in whatever order they are listed, so, if they are not listed in consecutive order, you get things like this:
x=circshift(linspace(-1,0.75,50),-1);
y=x.^2;
plot(x,y,'-o'); axis padded
>> issorted(Simulated_data_elements_unconnected_frequency,'ascend')
ans =
logical
0
Hmm it does look like that the x values are not in ascending order. Do you know how to check where the discontinuity is?
Let's make some sample data:
x = [1 2 3 4 3 4 5 1]
x = 1×8
1 2 3 4 3 4 5 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ind = find(x(2:end) <= x(1:end-1))
ind = 1×2
4 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
check = [x(ind); x(ind+1)].'
check = 2×2
4 3 5 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x(4) [the first element of ind] is indeed greater than x(5), as the first row of check shows. Similarly x(7) is greater than x(8) as shown in the second row of check.
Blayze
Blayze on 26 Apr 2026 at 3:59
Edited: Blayze on 26 Apr 2026 at 4:00
Thanks for the help everyone, it looks like one of the text files contained multiple sets of data, which is why the functions kept indicating the data for the x values was not always increasing. The plot now contains no extraneous lines.

Sign in to comment.

More Answers (1)

Try something like this with your data
[sortedx, sortOrder] = sort(x, 'ascend'); % Sort x and get the order it was sorted in.
sortedy = y(sortOrder); % Sort y the same way so the y stays matched up with its original x.
plot(sortedx, sortedy, 'b-', 'LineWidth', 2); % Plot the sorted data.

Asked:

on 25 Apr 2026 at 22:54

Edited:

on 26 Apr 2026 at 4:00

Community Treasure Hunt

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

Start Hunting!