Removing a line between two specific data points in Matlab

I am going to draw a graph in Matlab. The graph is pretty simple and I use the graph function. Suppose the data I want to build is (0:1:10) . I also put markers on my chart. Then we have a line with markers in the coordinates (0,0),(1,1),(2,2),... etc.

Now I want to delete the line between (2,2) and (3,3) without deleting the whole line. That is, my goal is to get rid of a certain segment of the line without losing the entire line or any points of the marker.

How can i do this?

+4
source share
2 answers

Try the following:

 y = [0.2751 0.2494 0.1480 0.2419 0.2385 0.1295 0.2346 0.1661 0.1111]; x = 1:numel(y); plot(x(1:4), y(1:4), '-x') hold plot(x(5:end), y(5:end), '-x') 

Graph result

+2
source

Deleting a row section after you have built it is difficult. You can see that the string consists of one MATLAB object with the following code:

 x = 1:10; y = 1:10; H = plot(x, y, '-o'); get(H, 'children') 

ans =

Empty Matrix: 0-by-1

We see that the line has no children, so there is no β€œsub-part” that we can remove. However, there are some cheeky tricks that we can use to try to achieve the same effect.


Separate two lines separately

... using hold on . See Victor Hugo's answer. This is the right way to achieve our goal.


Separate two separate lines in one

MATLAB does not dial points with a value of NaN. By modifying the input vectors, you can make MATLAB skip the point to give a dashed line effect:

 x = [0 1 2 2 3 4 5 6 7 8 9]; y = [0 1 2 nan 3 4 5 6 7 8 9]; plot(x, y, '-o'); 

This is equivalent to plotting the line from [0, 0] to [2, 2], skipping the next point, then starting it again in [3, 3] and continuing [9, 9].

enter image description here


Erase part of a line

This is the nastiest way to do this, but it is a cheap hack that can work if you are not worried about changing your input arrays. First draw a line:

 x = 1:10; y = 1:10; plot(x, y, '-o'); 

enter image description here

Now draw a white line over the part you want to erase:

 hold on plot([2 3], [2 3], 'w'); 

enter image description here

As you can see, the result is not entirely correct and will respond poorly if you try to do other things on the chart. In short, I would not recommend this method, but it can come in handy in desperate times!

+5
source

Source: https://habr.com/ru/post/1438104/


All Articles